简体   繁体   English

android ListView ArrayAdapter

[英]android ListView ArrayAdapter

i'm using an ArrayAdapter in my ListView. 我在ListView中使用ArrayAdapter。

I can't really understand if when i'm setting and ArrayList to my ArrayAdapter, it recreate all the objects of it, or just reference to it. 我无法真正理解是否在将ArrayList设置为ArrayAdapter时重新创建它的所有对象,或者只是对其进行引用。

for some reason same objects which are supposed to be the same in too list views, doesn't get effected with property changes as if the current object is a clone of the other object.. 由于某些原因,在太列表视图中应该相同的相同对象,不会像当前对象是另一个对象的克隆一样受到属性更改的影响。

can anyone please clear that out ? 任何人都可以清除吗?

When you pass ArrayList to an ArrayAdapter , it actually manipulates object using Pass-by Reference . 当您将ArrayList传递给ArrayAdapter ,它实际上使用Pass-by Reference操纵对象。

And when you alter the contents of your ArrayList and want to reflect those changes in ArrayAdapter , then you must need to call notifyDataSetChanged() method of your adapter. 而且,当您更改ArrayList的内容并想在ArrayAdapter反映这些更改时,则必须调用适配器的notifyDataSetChanged()方法。

PS: Practically you should pass a cloned object of your ArrayList to adapter (unless you know what you are doing), since its unnecessary and will take extra resources. PS:实际上,您应该将ArrayList的克隆对象传递给适配器(除非您知道自己在做什么),因为它是不必要的,并且会占用额外的资源。

More info . 更多信息

It depends on how you create the ArrayAdapter . 这取决于您如何创建ArrayAdapter

If you use a version of the constructor that does not take an array or List as a parameter, then it will create a new list that each adapter instance maintains. 如果您使用的构造函数版本不使用数组或List作为参数,则它将创建一个新列表,每个适配器实例都将维护该列表。 If you create your adapter with one of the following versions: 如果使用以下版本之一创建适配器:

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Then you can supply the same external list to multiple adapters and change your data set only once, because in this case the adapter just references your list and doesn't copy the data. 然后,您可以将同一外部列表提供给多个适配器,并且只更改一次数据集,因为在这种情况下,适配器仅引用您的列表,而不复制数据。

两种情况都可能发生,在这里说明第二种情况如何帮助优化列表视图: http : //developer.android.com/training/improving-layouts/smooth-scrolling.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM