Android -adapter.notifyDataSetInvalidated 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3219779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:12:27  来源:igfitidea点击:

Android - What does adapter.notifyDataSetInvalidated do?

androiddataadapter

提问by Tawani

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

方法adapter.notifyDataSetInvalidated() 完成了什么?没有关于它的文档。

I am trying to reload a ListView and notifyDataSetChanged or notifyDataSetInvalidated don't seem to accomplish anything.

我正在尝试重新加载 ListView 并且 notifyDataSetChanged 或 notifyDataSetInvalidated 似乎没有完成任何事情。

采纳答案by Cristian

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 DataSetObservableclass (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...
  1. notifyDataSetInvalidated()调用notifyInvalidated()的的DataSetObservable类(见这里
  2. 然后,为每个notifyInvalidated()调用该onInvalidated()方法DataSetObserver请参阅此处)。
  3. 然后是有趣的部分:onInvalidated()什么都不做......

This is its implementation:

这是它的实现:

public void onInvalidated() {
    // Do nothing
}

DataSetObserveris an abstract class, so it's up to the subclass to decide what to do on onInvalidated().

DataSetObserver是一个抽象类,因此由子类决定在 上做什么onInvalidated()

回答by lucidd

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 ListViewso you can see the new data added, but you have to call it in the UI thread.

据我所知,该notifyDataSetInvalidated()方法会阻止适配器访问数据(以防数据无效、不可用等)。该notifyDataSetChanged()方法更新ListView以便您可以看到添加的新数据,但您必须在 UI 线程中调用它。

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

观看此视频对我帮助很大——他们在两节中提到了这些方法并解释了如何正确使用它们。也许它对你也有帮助:)

回答by Tony Chan

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

我最近遇到了这个问题,并想为那些以编程方式想知道调用notifyDataSetChanged()和时发生了什么的人详细说明notifyDataSetInvalidated()。*简短的回答,去这里

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.

正如@Cristian 在他的回答中所述,当您在 Adapter 上调用这些通知方法时,它基本上会通过几个类进行调用,并最终在为您的 Adapter注册的s上调用onChanged()/ 。onInvalidated()DataSetObserver

If you follow the code you will indeed see that DataSetObserveris abstract as stated, and that the onChanged()/onInvalidated()methods are empty waiting for implementation by a subclass.

如果您按照代码进行操作,您确实会看到它DataSetObserver是抽象的,并且onChanged()/onInvalidated()方法是空的,等待子类的实现。

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 DataSetObservercalled AdapterDataSetObserverand it lives in the abstract class AdapterView(which is extended by classes like GridViewand ListView). This observer is automatically created by Android when you setAdapter()on your AdapterViewand gets registeredto your Adapter.

如果这就是故事的结局,那么为什么 Android 框架工程师一直告诉我们如果他们什么都不做就调用这些方法呢?它采取了一些挖掘,但事实证明,已经有这方面的一个子类DataSetObserverAdapterDataSetObserver,它生活在抽象类AdapterView(这是由类,如扩展GridViewListView)。当您setAdapter()在您的适配器上注册AdapterView注册到您的适配器时,此观察器由 Android 自动创建。

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 DataSetObserverto get these functioning, but turns out one is already created for you.

在这里你可以看到这些方法实际上所做的所有疯狂的事情。文档很差,起初我以为我需要注册我自己的子类DataSetObserver才能获得这些功能,但事实证明已经为您创建了一个。

*Something I thought might be useful: I believe that you can register more than one DataSetObserverto 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).

*我认为可能有用的东西:我相信您可以DataSetObserver向您的适配器注册多个(除了默认的)。这将允许您在需要时做一些额外的工作(例如,当位图下载完成时,可能会将进度条视图与图像视图交换)。

回答by android developer

According to the "the world of listView" lecture, you should use it each time the listView has nothing to show (meaning empty data).

根据“ listView 的世界”讲座,您应该在每次 listView 没有可显示内容(即空数据)时使用它。

One example they talk about is when the filtering is done (on "publishResults" method). on the lecture video, it's on 36:00 .

他们谈论的一个例子是过滤完成时(在“publishResults”方法上)。在讲座视频上,时间是 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...

奇怪的是,他们为什么不将它与notifyDataSetChanged合并,它可以检查项目的数量并决定自己调用它......

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.

据我所见,讲课讲的不太对。如果适配器之前显示了一些内容,现在它不包含任何内容,现在您调用 notifyDataSetInvalidated,则不会发生任何事情,因此内容将保留,因此我认为仅在数据未更改时使用 notifyDataSetInvalidated 是安全的.

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

例如,如果您处理过滤,并且得到与以前相同的结果(也许检查结果数量就足够了),您可以调用 notifyDataSetInvalidated 而不是 notifyDataSetChanged