Android What does the .notifyDataSetInvalidated adapter do?

What does the adapter.notifyDataSetInvalidated () method do? There is no documentation on it.

I am trying to reload a ListView and notifyDataSetChanged or notifyDataSetInvalidated seems to do nothing.

+28
android dataadapter
Jul 10 '10 at 15:27
source share
4 answers

It depends on the adapter implementation ... if you look at the source code, you will see that:

  • notifyDataSetInvalidated() calls the notifyInvalidated() of the DataSetObservable class ( see here )
  • Then notifyInvalidated() calls the onInvalidated() method for each DataSetObserver ( see here ).
  • Then the funny part comes in: onInvalidated() does nothing ...

This is its implementation:

 public void onInvalidated() { // Do nothing } 

DataSetObserver is an abstract class, so it is up to the subclass to decide what to do on onInvalidated() .

+26
Jul 10 '10 at 15:40
source share

As far as I know, the notifyDataSetInvalidated() method does not allow the adapter to access the data (if it is invalid, unavailable, etc.). The notifyDataSetChanged() method updates the ListView so you can see the new data, but you should call it in the user interface stream.

It helped me a lot to watch this video - there are two sections where they mention these methods and explain how to use them correctly. Maybe this helps too :)

+28
Jul 10 '10 at 15:50
source share

I recently came across this question and wanted to develop for those who are interested in programming what happens when you call notifyDataSetChanged() and notifyDataSetInvalidated() . * Short answer, go here

As @Cristian said in his answer, when you call these notification methods on your adapter, it basically calls several classes and ends the onChanged() / onInvalidated() call in the registered DataSetObserver for your adapter.

If you execute the code, you will really see that the DataSetObserver is abstract as indicated, and that the onChanged() / onInvalidated() methods are empty, waiting for the implementation to be subclassed.

If this was the end of the story, then why do Android framework developers keep telling us to call these methods if they don't do anything? It took some digging, but it turned out that a subclass of this DataSetObserver called AdapterDataSetObserver already exists, and it lives in the abstract class AdapterView (which extends with classes such as GridView and ListView ). This observer is automatically created by Android when you setAdapter() on your AdapterView and get registered on your adapter.

Here you can see all the crazy things that these methods actually do. The documentation is bad, and at first it seemed to me that I needed to register my own subclass DataSetObserver in order to get these functions, but it turns out that one of them has already been created for you.

* Something I thought might be useful: I believe that you can register more than one DataSetObserver for your adapter (in addition to the default). This will allow you to do some additional work if necessary (for example, it is possible to change the position of the progress bar with the image when loading raster images).

+18
Oct 12 '11 at 0:27
source share

According to the lecture in the world of ListView, you should use it every time the listView has nothing to show (which means empty data).

One of the examples they are talking about is when filtering is done (in the publishResults method). in the lecture video, he is at 36:00.

It is strange why they simply did not combine it with notifyDataSetChanged, which could check the number of elements and accept the call on its own ...

In accordance with what I saw, what was said at the lecture is not entirely true. if the adapter has already shown some content before, and now it contains nothing, and now you call notifyDataSetInvalidated, nothing will happen, so the contents will remain, so I consider it safe to use notifyDataSetInvalidated only when the data does not change,

For example, if you process the filtering and get the same results (and maybe enough to check the number of results) as before, you can call notifyDataSetInvalidated instead of notifyDataSetChanged

+3
Mar 22 '14 at 20:34
source share



All Articles