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.
It depends on the adapter implementation... if you take a look of the source code you will see that:
notifyDataSetInvalidated()
calls the notifyInvalidated()
of the DataSetObservable
class (see here)notifyInvalidated()
calls the onInvalidated()
method for each DataSetObserver
(see here).onInvalidated()
does not do anything...This is its implementation:
public void onInvalidated() {
// Do nothing
}
DataSetObserver
is an abstract class, so it's up to the subclass to decide what to do on onInvalidated()
.
As far as I know, the notifyDataSetInvalidated()
method stops the adapter from accessing the data (in case it's invalid, unavailable, etc.). The notifyDataSetChanged()
method updates the ListView
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 :)
It depends on the adapter implementation... if you take a look of the source code you will see that:
notifyDataSetInvalidated()
calls the notifyInvalidated()
of the DataSetObservable
class (see here)notifyInvalidated()
calls the onInvalidated()
method for each DataSetObserver
(see here).onInvalidated()
does not do anything...This is its implementation:
public void onInvalidated() {
// Do nothing
}
DataSetObserver
is an abstract class, so it's up to the subclass to decide what to do on onInvalidated()
.
notifyDataSetChanged()
instead of invalidated - changed will cause the adapter to redraw itself. –
Farriery I recently ran into this question and wanted to elaborate for those who are wondering programmatically what is happening when you call notifyDataSetChanged()
and notifyDataSetInvalidated()
. *Short answer, go here
As @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 registered DataSetObserver
s for your Adapter.
If you follow the code you will indeed see that DataSetObserver
is abstract as stated, and that the onChanged()
/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
called AdapterDataSetObserver
and it lives in the abstract class AdapterView
(which is extended by classes like GridView
and ListView
). This observer is automatically created by Android when you setAdapter()
on your AdapterView
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
© 2022 - 2024 — McMap. All rights reserved.
notifyDataSetChanged()
instead of invalidated - changed will cause the adapter to redraw itself. – Farriery