What does the method adapter.notifyDataSetInvalidated() accomplish? There is no documentation on it.
I am trying to reload a ListView and notifyDataSetChanged or notifyDataSetInvalidated don't seem to accomplish anything.
What does the method adapter.notifyDataSetInvalidated() accomplish? There is no documentation on it.
I am trying to reload a ListView and notifyDataSetChanged or notifyDataSetInvalidated don't seem to accomplish anything.
As far as I know, the
notifyDataSetInvalidated()
method stops the adapter from accessing the data (in case it's invalid, unavailable, etc.). ThenotifyDataSetChanged()
method updates theListView
so you can see the new data added, but you have to call it in the UI thread.It helped me a lot to watch this video -- there are two sections where they mention those methods and explain how to use them correctly. Maybe it helps you too :)
I recently ran into this question and wanted to elaborate for those who are wondering programmatically what is happening when you call
notifyDataSetChanged()
andnotifyDataSetInvalidated()
. *Short answer, go hereAs @Cristian stated in his answer, when you call these notify methods on your Adapter it basically calls through a few classes and ends up calling
onChanged()
/onInvalidated()
on the registeredDataSetObserver
s for your Adapter.If you follow the code you will indeed see that
DataSetObserver
is abstract as stated, and that theonChanged()
/onInvalidated()
methods are empty waiting for implementation by a subclass.If this were the end of the story, then why do Android framework engineers keep telling us to call these methods if they do nothing? It took some digging but it turns out there is already a subclass of this
DataSetObserver
calledAdapterDataSetObserver
and it lives in the abstract classAdapterView
(which is extended by classes likeGridView
andListView
). This observer is automatically created by Android when yousetAdapter()
on yourAdapterView
and gets registered to your Adapter.It is here that you can see all the crazy stuff these methods actually do do. The documentation is poor and at first I thought I needed to register my own subclassed
DataSetObserver
to get these functioning, but turns out one is already created for you.*Something I thought might be useful: I believe that you can register more than one
DataSetObserver
to your Adapter (in addition to the default one). This would allow you to do some extra work if needed (like maybe swap a progress bar view with an image view when bitmaps are done downloading).According to the "the world of listView" lecture, you should use it each time the listView has nothing to show (meaning empty data).
One example they talk about is when the filtering is done (on "publishResults" method). on the lecture video, it's on 36:00 .
The weird thing is, why didn't they just merge it with notifyDataSetChanged, which could check for the number of the items and decide to call it by itself...
According to what I've seen, what was talked on the lecture isn't quite right. if the adapter has shown some content before, and now it doesn't contain anything, and now you call notifyDataSetInvalidated, nothing will happen, so the content will remain, so I think it's safe to use notifyDataSetInvalidated only when the data doesn't change.
For example, if you handle the filtering, and you get the same results (and maybe it's enough to check the number of results) as before, you can call notifyDataSetInvalidated instead of notifyDataSetChanged
It depends on the adapter implementation... if you take a look of the source code you will see that:
notifyDataSetInvalidated()
calls thenotifyInvalidated()
of theDataSetObservable
class (see here)notifyInvalidated()
calls theonInvalidated()
method for eachDataSetObserver
(see here).onInvalidated()
does not do anything...This is its implementation:
DataSetObserver
is an abstract class, so it's up to the subclass to decide what to do ononInvalidated()
.