Google I/O 2010 - ListView 的世界 notifyDataSetChanged()
抱歉提出了愚蠢的问题。但确实很有趣而且难以理解。本次会议讨论了 notifyDataSetChanged()
方法。
从该方法的文档来看 - “当被观察的数据集发生变化时调用,并且在读取时包含数据的新状态”。我的英语不好,全部听不懂。但如果我猜想当我需要使用新数据集刷新 ListView
时调用该方法,我是对的?
如果我是对的那我就很困惑了。在过去和我的第一个程序中,我使用了 android 的联系人 api。并在asynctask
中运行一些处理。这时出现了带有进度条的对话框,在后台可以看到ListView
的状态实时变化。 ListView 行的数据通过 BindView
更改。
为什么?所以我出了问题。请解释一下。
sorry for stupid question. But really interesting and incomprehensible. In this session discussed about notifyDataSetChanged()
method.
From documentation for this method - "called when the data set being observed has changed, and which when read contains the new state of the data". My English bad and I do not understand all. But I right if guess that method called when I need refresh ListView
with new data set?
If I'm right then I'm confused. In the past and my first program I played with contacts api of android. And run some processing in an asynctask
. At this time appeared dialog with progress bar and in the background, you could see how the state of ListView
changed in real time. Data for ListView row changed via BindView
.
Why? So I'm in something wrong. Explain please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我阅读,BindView 仅与游标一起使用,游标基本上是数据集的特定类型。您可以有替代数据集,例如 API 中的 ArrayListAdapter 使用 ArrayList 作为其数据集。如果数据集发生变化,则必须调用notifyDataSetChanged()来通知列表视图必须重新计算其边界并且必须重新绘制其视图(可能还有更多)。
如果您决定编写自己的适配器并创建通过适配器修改列表视图中显示的数据的可能性(例如,可以想象在您自制的适配器中添加像 addObject(SomeObject o) 这样的方法),那么您可以调用notifyDataSetChanged () 在该方法中。
类似地,如果你有一个deleteObject(SomeObject x),如果剩余数据集大于零,你会调用notifyDataSetChanged(),或者当剩余数据集为空时,你会调用notifyDataSetInvalidated(),这反过来会产生一些额外的东西就像在列表中设置所谓的空视图(如果您已指定)一样。
As i read it, BindView is only used with cursors, which are a specific type of a data set basically. You can have alternative data sets, there is for example an ArrayListAdapter in the API which uses an ArrayList as its dataset. In case that data set changes, notifyDataSetChanged() will have to be called to notify the list view that its bounds will have to be recalculated and its views have to be redrawn (and probably some more).
If you decide to write your own and create the possibility to modify the data shown in the list view through an adapter (one could imagine adding method like addObject(SomeObject o) in your home made adapter for example), then you'd call notifyDataSetChanged() in that method.
Similarly if you have a deleteObject(SomeObject x), if the remaining data set is larger than zero you'd call notifyDataSetChanged() or when the remaining data set is empty you'd call notifyDataSetInvalidated() which in turn will to some extra stuff like setting the so called empty view in the list if you have one specified.