Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

Android - Delete an item from ListViewAdapter and then Refresh

I'm trying to delete an item from my ListViewAdapter, I've implemented my setOnItemLongClickListener doing this :

getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                            @Override
                            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                                           int arg2, long arg3) {
                                Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show();


                                return true;
                            }
                        });

It shows me a Toast, verifying that it detects the onClickListener, I try to remove it doing : mItems.remove(arg2);, but nothing happens... I was looking for the correct answer and I found that I had to call notifyDataSetChanged(), but since I couldn't call this method I tried this one :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.ofertasRefresh:
            mItems.notifyAll();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

but nothing happens, no error on LogCat, no app crashes, no nothing... Can you guys tell me what I'm missing?

Upvotes: 0

Views: 73

Answers (3)

Bryan Herbst
Bryan Herbst

Reputation: 67189

The first step is to store your adapter in a member variable so that you can access it again later.

Instead of calling setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));, change your Fragment to look something like this:

public class MyFragment extends ListFragment {
    private Adapter mListAdapter;

    @Override
    public voidonCreate(Bundle savedInstanceState) {
        mListAdapter = new ListViewDemoAdapter(getActivity(), mItems);
        setListAdapter(mListAdapter);
    }

}

Then when you remove an item, you can call mListAdapter.notifyDataSetChanged() so the list knows it needs to redraw.

notifyAll() isn't remotely close to notifyDataSetChanged().

Upvotes: 1

encastellano
encastellano

Reputation: 445

You need to declare your adapter global in your activity like this:

ListViewDemoAdapter adapter;

Put:

adapter = new ListViewDemoAdapter(getActivity(), mItems)); setListAdapter(adapter);

instead of:

setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));

and when your removed item, you should call:

adapter.notifyDataSetChanged();

Upvotes: 1

Amit K. Saha
Amit K. Saha

Reputation: 5951

you should do like this-

// as class member

ListViewDemoAdapter myAdapter;

then in the method-

myAdapter = new ListViewDemoAdapter(getActivity(), mItems);
setListAdapter(myAdapter);

now in the onItemClickListener,

mItems.remove(arg2);
myAdapter = new ListViewDemoAdapter(getActivity(), mItems);
setListAdapter(myAdapter);

note that, this is not the best solution, but it should work just fine for your case.

Upvotes: 2

Related Questions