[英]When should I use BaseAdapter in Android?
I am working on android app in which I am using adapters to populate the data in a listview. 我正在使用适配器在列表视图中填充数据的android应用程序上工作。 I am confused where we should use
BaseAdapter
. 我在应该使用
BaseAdapter
地方感到困惑。 I read many questions where it is written that we should use ArrayAdapter
for arrays and arraylist that is ok and CursorAdapter
in case of cursor. 我读到很多问题,写在哪里,我们应该将
ArrayAdapter
用于可以正常使用的数组和arraylist,并在使用游标的情况下使用CursorAdapter
。
I know BaseAdapter
is the super class of ArrayAdapter
and CursorAdapter
. 我知道
BaseAdapter
是ArrayAdapter
和CursorAdapter
的超类。 I have checked already this question What is the difference between ArrayAdapter , BaseAdapter and ListAdapter but it don't explain when we should use BaseAdapter
. 我已经检查了这个问题ArrayAdapter,BaseAdapter和ListAdapter之间有什么区别,但是没有解释我们何时应该使用
BaseAdapter
。
When should I use BaseAdapter
? 什么时候应该使用
BaseAdapter
?
You should use it: 您应该使用它:
if your model data is not already in a data structure for which there is a concrete ListAdapter
class, and 如果您的模型数据尚未在具有具体
ListAdapter
类的数据结构中,并且
if you determine that creating a custom adapter will be better for the user, or perhaps less development work for you, than would be reorganizing your data structure 如果您确定创建自定义适配器将比重组数据结构对用户更好,或者对您的开发工作更少
For example, suppose that you use JSONArray
to parse a snippet of JSON. 例如,假设您使用
JSONArray
来解析JSON片段。 JSONArray
does not implement the List
interface, and therefore you cannot use it with ArrayAdapter
. JSONArray
不实现List
接口,因此不能与ArrayAdapter
一起使用。 None of the other adapters match. 其他适配器均不匹配。 Yet, you want to show this
JSONArray
in an AdapterView
. 但是,您想在
AdapterView
显示此JSONArray
。 In that case, your choices are: 在这种情况下,您的选择是:
roll through the data and convert it into an ArrayList
, so you can use ArrayAdapter
, or 滚动数据并将其转换为
ArrayList
,因此可以使用ArrayAdapter
,或者
create a custom subclass of BaseAdapter
that can adapt a JSONArray
(a JSONArrayAdapter
) 创建可以适配
JSONArray
( JSONArrayAdapter
)的BaseAdapter
的自定义子类
stop using JSONArray
and instead use something else for parsing your JSON, like Gson, which can populate a List
directly, allowing you to use ArrayAdapter
停止使用
JSONArray
,而是使用其他内容来解析JSON,例如Gson,它可以直接填充List
,从而允许您使用ArrayAdapter
If your data is available in a Collection
you can go with an ArrayAdapter
. 如果您的数据在
Collection
可用,则可以使用ArrayAdapter
。 (use the addAll
method to put your data in the adapter) (使用
addAll
方法将数据放入适配器)
If your data is not in a Collection
: then you can't and you must find another adapter that suits your needs. 如果您的数据不在
Collection
则您不能,您必须找到另一个适合您的适配器。 (typically: use a CursorAdapter
when data comes from a database) (通常:当数据来自数据库时,使用
CursorAdapter
)
If you can't find any existing adapter working fine with your data structure/data source : you can write a subclass of BaseAdapter
to support your own data structure. 如果找不到任何适用于您的数据结构/数据源的现有适配器:可以编写
BaseAdapter
的子类来支持您自己的数据结构。
If you plan to display your data in a ListView
(this is a common use-case): then you must ensure that your adpter also implements ListAdapter
(because the ListView
needs a ListAdapter
). 如果您打算在
ListView
显示数据(这是一个常见的用例):那么您必须确保您的ListAdapter
也实现了ListAdapter
(因为ListView
需要一个ListAdapter
)。
Note that ArrayAdapter
and CursorAdapter
implements ListAdapter
. 请注意,
ArrayAdapter
和CursorAdapter
实现ListAdapter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.