cofudayffy
cofudayffy

Reputation: 43

notifyDataSetChanged in a listfragment does nothing

I have a listfragment with an adapter define like this :

ArrayAdapter<String> aa;
    ArrayList<String> arrayList = new ArrayList<String>();

aa = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,  arrayList);
        setListAdapter(aa);

I 'm using this to add item to my list from an sqlite database

arrayList.add

the problem is that notify data has change does nothing (i m deleting element from the sqlite so i need to refresh the content of the arraylist)

i'm doing this :

aa.notifyDataSetChanged();

Upvotes: 0

Views: 1038

Answers (1)

Piotr Wittchen
Piotr Wittchen

Reputation: 3922

You can find a StackOverflow anwser explaining in which case notifyDataSetChanged() method works. Here is quote from that answer:

For an ArrayAdapter, notifyDataSetChanged only works if you use the add(), insert(), remove(), and clear() on the Adapter.

I guess you are performing your operations on a database, but you are not performing any operations on the Adapter object. In your case, you should probably set Adapter every time, when you want to load fresh data. You can use your existing code for this:

adapter = new ArrayAdapter<String>(getActivity(),
     android.R.layout.simple_list_item_1,  arrayList);

setListAdapter(adapter);

In addition, you can wrap this code into a method like:

void refreshAdapter(ArrayList<String> arrayList);

and use it every time when you want to refresh your adapter.

Upvotes: 1

Related Questions