java 当调用 ArrayAdapter 中的 getView() 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10160475/
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-10-30 23:52:36  来源:igfitidea点击:

When getView() in ArrayAdapter is called

javaandroidlayoutadapterandroid-arrayadapter

提问by Adham

When creating a customized adapterfor ListViewin android, I see that I have to create a class the extends ArrayAdapterclass and implements the getView(..)method.

当创建一个定制adapterListViewandroid系统中,我看到,我要创建一个类的扩展ArrayAdapter类并实现getView(..)方法。

All of that is OK, but I want to know the sequence of calling methods and executing. i.e. in which point of code the getView()is being called ?

所有这些都可以,但我想知道调用方法和执行的顺序。即在哪个代码点getView()被调用?

采纳答案by Anamoy

From android docs - An Adapter object acts as a bridge between an AdapterView (such as ListView in your case) and the underlying data for that view. The Adapter provides access to the data items and is also responsible for making a View for each item in the data set.

来自 android docs - Adapter 对象充当 AdapterView(例如您的案例中的 ListView)和该视图的基础数据之间的桥梁。Adapter 提供对数据项的访问,还负责为数据集中的每个项创建一个视图。

So, whenever the ListView needs to display a particular row of data, it requests the associated adapter to provide the view corresponding to that the data at that position through getView() method. This may occur whenever the view needs to be updated on screen (eg. during creation/scroll etc.).

因此,每当 ListView 需要显示特定行的数据时,它都会通过 getView() 方法请求关联的适配器提供与该位置的数据对应的视图。每当需要在屏幕上更新视图时(例如,在创建/滚动期间等),就会发生这种情况。

As an app developer, you need not worry about exactly at which point getView() is being called as long as you provide a concrete getView() implementation in your adapter. Make sure the method is efficient (thumbnails etc should be loaded in a background thread) for optimum performance.

作为应用程序开发人员,只要您在适配器中提供具体的 getView() 实现,您就不必担心确切地调用 getView() 的时间点。确保该方法有效(缩略图等应加载到后台线程中)以获得最佳性能。

回答by Kri

getView() of ArrayAdapter is called multiple times....

ArrayAdapter 的 getView() 被多次调用......

  1. as an when the new row is added...
  2. you scroll up and scroll down the list view....
  3. when the list is notfiedchanged..
  1. 当添加新行时...
  2. 您向上滚动并向下滚动列表视图....
  3. 当列表未更改时..

Refer this link Android custom ArrayAdapter getView method called multiple times - resetting dynamic TextView value

请参阅此链接 多次调用Android自定义ArrayAdapter getView方法-重置动态TextView值

回答by Its not blank

getView(int position, View view, ViewGroup parent) 

is called for the

被称为

List of Objects

of the nos of elements using

使用的元素数

getItem(int position)

for the length of our List

对于我们列表的长度

so, it is called nos of times untill your entire list has been allocated a layout and data from list.

因此,在您的整个列表已从列表中分配布局和数据之前,它被称为多次。