Android 在 CursorAdapter 中 bindView() 和 newView() 做什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12672749/
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
What bindView() and newView() do in CursorAdapter
提问by Jainendra
I have a custom CursorAdaptor
in my project with overridden methods bindView(View view, Context context, Cursor cursor)
and newView(View view, Context context, Cursor cursor)
.
I want to know for what CursorAdapter
are used for and what is the difference between and uses of overriding bindView()
and newView()
.
我CursorAdaptor
在我的项目中有一个自定义的覆盖方法bindView(View view, Context context, Cursor cursor)
和newView(View view, Context context, Cursor cursor)
. 我想知道用于什么CursorAdapter
以及覆盖bindView()
和newView()
.
I have read the Developer Siteand thistutorial but still I didn't understood. As I'm a beginner to Android, please help me understand this concept.
回答by Vinay S Shenoy
In order to understand this, you must first understand how BaseAdapter
works, since CursorAdapter
is a subclass of BaseAdapter
.
为了理解这一点,您必须首先了解它的BaseAdapter
工作原理,因为CursorAdapter
是BaseAdapter
.
Android maintains a pool of views for a ListView
which it will give to you so you can reuse it instead of creating a new view each time.
Android 维护一个视图池,ListView
它将提供给您,以便您可以重复使用它,而不是每次都创建一个新视图。
In BaseAdapter
, you will have a function called getView()
, to which one of the parameters is a View
object named convertView
. Basically, this convertView
will be null
if the list is being loaded for the first time, and it will not be null
once you start sliding the list. Therefore, in the getView()
method of your BaseAdapter
, you will check if convertView
is null
. If yes, you will inflate it. Then you can use the view and set its elements as normal. This will improve the scrolling performance of a listview tremendously.
在 中BaseAdapter
,您将调用一个函数getView()
,其中一个参数是一个View
名为的对象convertView
。基本上,这convertView
将是null
第一次加载列表时,null
一旦开始滑动列表就不会。因此,在getView()
您的方法中BaseAdapter
,您将检查是否convertView
为null
。如果是,你会膨胀它。然后您可以使用该视图并将其元素设置为正常。这将极大地提高列表视图的滚动性能。
A CursorAdapter
makes it easy to use when the data source of a listview is a database. In a cursor adapter, however, Android takes care of checking whether the convertView
is null
or not. In the newView()
method, you simply inflate the view and return it. In the bindView()
method, you set the elements of your view.
CursorAdapter
当列表视图的数据源是数据库时,A很容易使用。在光标适配器,不过,Android负责检查的是否convertView
是null
或不是。在该newView()
方法中,您只需膨胀视图并返回它。在该bindView()
方法中,您设置视图的元素。
As an example, imagine a listview on a device which can show upto 11 list items on the screen. In this case, newView()
will be called upto 11 times. However, bindView()
will be called many times whenever you scroll the list view. The 11 views you created in your newView method will be reused again and again as you scroll the list.
例如,假设设备上的列表视图最多可以在屏幕上显示 11 个列表项。在这种情况下,newView()
最多将被调用 11 次。但是,bindView()
每当您滚动列表视图时,都会被多次调用。您在 newView 方法中创建的 11 个视图将在您滚动列表时一次又一次地重复使用。