Android: notifyDataSetChanged(); not working

One of the main reasons notifyDataSetChanged() won’t work for you – is,

Your adapter loses reference to your list.

When you first initialize the Adapter it takes a reference of your arrayList and passes it to its superclass. But if you reinitialize your existing arrayList it loses the reference, and hence, the communication channel with Adapter.

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 without checking for null and empty values. Set the adapter to the list directly (don’t check for any condition). Adapter guarantees you that wherever you make changes to the data of the arrayList it will take care of it, but never
    lose the reference.
  3. Always modify the data in the arrayList itself (if your data is completely new then you can call adapter.clear() and arrayList.clear() before actually adding data to the list) but don’t set the adapter i.e If the new data is populated in the arrayList than just adapter.notifyDataSetChanged()

Stay true to the Documentation.

Leave a Comment