notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set. See also: ArrayAdapter.
CursorAdapter is a subclass of BaseAdapter. It is used to bind cursor data qureied from database to AdapterView. Cursor adapter has following abstract methods, newView(Context context, Cursor cursor, ViewGroup parent) : View This method creates new AdapterView to hold data.
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()
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With