Android系统。如何notifyDataSetChanged()方法和列表视图的工作? [英] Android. How does notifyDataSetChanged() method and ListViews work?

查看:127
本文介绍了Android系统。如何notifyDataSetChanged()方法和列表视图的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的ListView 概念以及它是如何工作的,我想创建自己的适配器,它扩展了 BaseAdapter 。对于 ArrayAdapter 例如,有 notifyDataSetChanged()方法,它应该被称为后,你已经更新了数组列表它保存所有数据,以刷新的ListView

I am trying to understand the ListView concept and how it works and I'm trying to create my own adapter which extends BaseAdapter. For ArrayAdapter for instance, there is the notifyDataSetChanged() method which should be called after you've updated the array list which holds all your data, in order to refresh the ListView.

不过,我创造我自己的 BaseAdapter 子类。这种方法是不提供给我的,是吗?我如何实现此方法?基本上,这是什么方法做完全,也许我会理解的话。

But I am creating my own subclass of BaseAdapter. That method is not available to me, or is it? How do i implement this method? Basically, what does that method do exactly, maybe I'll understand then.

在情况下, ArrayAdapter 我猜它着眼于什么位置的ListView 正在显示它的检查是否是相同的一个作为在的ArrayList 后,这是更新?还是......

In case of the ArrayAdapter i'm guessing it looks at what position the ListView is currently displaying and it checks if it's the same one as in the ArrayList after it was updated? Or...

它说,该方法:

通知所附的观察员的基础数据已   改变,任何视图反映了数据集应自动刷新。

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

不过,究竟如何不会刷新自己?

But how exactly does it refresh itself?

有人能解释吗?

推荐答案

我已经想通了。我怎么也想不通到底适配器开始,又是如何知道从哪里获取数据。当我延长了 BaseAdapter 类,在类的构造函数我初始化的项目,我想看到​​的ListView 。但我无法弄清楚如何将这些值会被使用时。

I've figured it out. I couldn't understand how the hell the adapter started and how did it know where to get the data from. When i extended the BaseAdapter class, in the constructor of that class I initialized the list of items that I wanted to see in the ListView. But I couldn't figure out how these values would be used and when.

所以这里的东西! :

BaseAdapter 还有需要重写一些方法。这其中,有 getCount将()

In the BaseAdapter there are some methods that need to be overridden. Among these, there is getCount().

的ListView 创建和诸如此类的东西,它会调用 getCount将()。如果返回值不是0(我回来我已经$ P $在构造函数初始化pviously ArrayList的大小),然后调用 getView()足够次,以填充项屏幕。 例如,我初始化的ArrayList 20项。因为只有8个项目初步适应屏幕上, getView()被称为8次,每次都询问这需要我回来(更多precisely它的位置想知道如何行会看起来像列表上的特定位置,什么样的数据,它需要含)。如果我向下滚动列表, getView()被调用一遍又一遍,直到我打了列表的末尾,在我的情况下,20件/行。

When the ListView is created and whatnot, it calls getCount(). If this returns a value different than 0 (I returned the size of the ArrayList which I've previously initialized in the constructor), then it calls getView() enough times to fill the screen with items. For instance, I initialized the ArrayList with 20 items. Because only 8 items initially fit on the screen, getView() was called 8 times, each time asking for the position it required for me to return (more precisely it wanted to know how the row would look like in the list on that specific position, what data it needed to contain). If I scroll down the list, getView() gets called over and over again, 'til I hit the end of the list, in my case 20 items / rows.

什么 notifyDataSetChanged()确实是......叫时,它会在哪些项目在其呼叫(更多precisely此刻都显示在屏幕上其中排索引)并调用 getView()与这些职位。

What notifyDataSetChanged() does is ... when called, it looks at what items are displayed on the screen at the moment of its call (more precisely which row indexes ) and calls getView() with those positions.

如果您正在显示第8项清单(所以这些都是那些在屏幕上可见),并在列表中的第2和第3项之间添加另一个项目,你叫 notifyDataSetChanged()然后 getView()被称为8倍,与位置从0开始,以7结尾,和因为在 getView()方法,你得到的数据的的ArrayList 然后它会自动返回插入新项目在旁边7列表出来的previous 8(7而不是8,因为最后一个项目接着一个位置了,所以它是不可见的了),而的ListView 将重绘,或什么的,这些项目。

i.e. if you're displaying the first 8 items in the list (so those are the ones visible on the screen) and you add another item between the 2nd and 3rd item in the list and you call notifyDataSetChanged() then getView() is called 8 times, with positions starting from 0 and ending with 7, and because in the getView() method you're getting data from the ArrayList then it will automatically return the new item inserted in the list alongside 7 out of the previous 8 (7 and not 8 because the last item went one position down, so it is not visible anymore), and the ListView will redraw, or whatever, with these items.

此外,重要的是要说明的是,如果你已经实现 getView()正确,你将最终回收已经显示的项(对象)(而不是创建新的)。请参阅 视频 在大约12:00分钟看到实施的正确方法 getView()

Also, important to specify is that if you've implemented getView() correctly, you'll end up recycling the items (the objects) already displayed (instead of creating new ones). See this video at around 12:00 minutes to see the correct way to implement getView()

我已经想通了这一切通过拨打电话到 LogCat中在每一个方法和下面发生了什么事情。

I've figured all this out by placing calls to LogCat in every method and following what was going on.

希望这可以帮助别人谁是刚刚开始了解如何的ListView 工作

Hope this helps someone who's just now starting to understand how ListViews work.

P.S。 这个例子 也帮了我很多理解。

P.S. This example also helped me a lot to understand.

这篇关于Android系统。如何notifyDataSetChanged()方法和列表视图的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆