Android - What does adapter.notifyDataSetInvalidated do?
Asked Answered
P

4

29

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.

Prosecute answered 10/7, 2010 at 15:27 Comment(0)
B
26

It depends on the adapter implementation... if you take a look of the source code you will see that:

  1. notifyDataSetInvalidated() calls the notifyInvalidated() of the DataSetObservable class (see here)
  2. Then, notifyInvalidated() calls the onInvalidated() method for each DataSetObserver (see here).
  3. Then comes the funny part: 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().

Breakwater answered 10/7, 2010 at 15:40 Comment(1)
Note that you almost always want to call notifyDataSetChanged() instead of invalidated - changed will cause the adapter to redraw itself.Farriery
S
28

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 :)

Steiner answered 10/7, 2010 at 15:50 Comment(1)
Great video. Beeing more precised look for 17:35 - youtu.be/wDBM6wVEO70?t=17m35s , "the data is gone, we are not going to access it anymore" e.g the file was deleted. That nailed it for me.Quay
B
26

It depends on the adapter implementation... if you take a look of the source code you will see that:

  1. notifyDataSetInvalidated() calls the notifyInvalidated() of the DataSetObservable class (see here)
  2. Then, notifyInvalidated() calls the onInvalidated() method for each DataSetObserver (see here).
  3. Then comes the funny part: 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().

Breakwater answered 10/7, 2010 at 15:40 Comment(1)
Note that you almost always want to call notifyDataSetChanged() instead of invalidated - changed will cause the adapter to redraw itself.Farriery
F
20

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 DataSetObservers 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).

Fite answered 12/10, 2011 at 0:27 Comment(0)
E
3

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

Elda answered 22/3, 2014 at 20:34 Comment(2)
As your last paragraph, if you get the same results, you don't need to do anything, since the data is same as before :pFlimflam
@HendraWijayaDjiono That's what I wrote. It's weird as I don't see the point of ever using this. I can't find a good scenario that it's useful.Elda

© 2022 - 2024 — McMap. All rights reserved.