notifyDataSetChange not working from custom adapter notifyDataSetChange not working from custom adapter android android

notifyDataSetChange not working from custom adapter


Change your method from

public void updateReceiptsList(List<Receipt> newlist) {    receiptlist = newlist;    this.notifyDataSetChanged();}

To

public void updateReceiptsList(List<Receipt> newlist) {    receiptlist.clear();    receiptlist.addAll(newlist);    this.notifyDataSetChanged();}

So you keep the same object as your DataSet in your Adapter.


I have the same problem, and i realize that. When we create adapter and set it to listview, listview will point to object somewhere in memory which adapter hold, data in this object will show in listview.

adapter = new CustomAdapter(data);listview.setadapter(adapter);

if we create an object for adapter with another data again and notifydatasetchanged():

adapter = new CustomAdapter(anotherdata);adapter.notifyDataSetChanged();

this will do not affect to data in listview because the list is pointing to different object, this object does not know anything about new object in adapter, and notifyDataSetChanged() affect nothing.So we should change data in object and avoid to create a new object again for adapter


As I have already explained the reasons behind this issue and also how to handle it in a different answer thread Here. Still i am sharing the solution summary here.

One of the main reasons notifyDataSetChanged() won't work for you - is,

Your adapter loses reference to your list.

When creating and adding a new list to the Adapter. Always follow these guidelines:

  1. Initialise the arrayList while declaring it globally.
  2. Add the List to the adapter directly with out checking for null and emptyvalues . Set the adapter to the list directly (don't check for anycondition). Adapter guarantees you that wherever you makechanges to the data of the arrayList it will take care of it, but never loose thereference.
  3. Always modify the data in the arrayList itself (if your data is completely newthan you can call adapter.clear() and arrayList.clear() beforeactually adding data to the list) but don't set the adapter i.e Ifthe new data is populated in the arrayList than justadapter.notifyDataSetChanged()

Hope this helps.