Android Adapter

本文详细介绍了Android中ListView的几种适配器,包括ArrayAdapter、BaseAdapter等,并提供了具体的使用示例和优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android中有很多的适配器,首先看看这些适配器的继承结构

  这些适配器中,BaseAdapter用的最多,也用的最熟,先放过他,从ArrayAdapter开始

  一个listAdapter用来管理一个用一组任意对象的数组填充的ListView。默认的ListAdapter希望提供的ListView每一项的 xml布局配置文件中只有一个TextView,如果你想使用一个符合布局的话,你就要使用含有id字段的构造函数了,这个id要去引用这个复杂布局文件 中的一个TextView,TextView被引用了,使用数组中的对象,调用toString方法,转换成字符串来填充这个TextView,你可以使 用包含自定义对象的数组或者集合。重写自定义对象的toString()方法,来保证ListView显示。你也可以是使用其他的一些非TextView 控件来显示数组中的数据,例如ImageViews,通过重写Adapter的getView方法来得到你想要的view。

  构造函数:

  public ArrayAdapter (Context context, int textViewResourceId)

  context: The current context. 当期的上下文对象

  textViewResourceId: The resource ID for a layout file containing a TextView to use when instantiating views. 一个包含了TextView的布局xml文件的id,注意(这个布局文件里只能有TextView一个控件,TextView不能有父控件,否则会报错 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView)

  类似于这种的xml

  <?xml version="1.0" encoding="utf-8"?>
  <TextView android:id="@+id/subject"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_marginTop="5dip" android:textAppearance="?android:attr/textAppearanceMedium"
   android:singleLine="true" android:ellipsize="end" />

  public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

  objects:用来填充ListView,给ArrayAdapter提供数据的数组

  public ArrayAdapter (Context context, int textViewResourceId, List<T> objects) //建议使用这个,直接给ArrayAdapter填充了数据

  public ArrayAdapter (Context context, int resource, int textViewResourceId)

  这个是用来复杂布局的,ListView的Item项的布局文件中不止含有一个TextView控件

  resource: The resource ID for a layout file containing a layout to use when instantiating views. ListView中Item项的复杂布局xml文件

  textViewResourceId:The id of the TextView within the layout resource to be populated(显示) ListView中Item项的复杂布局xml文件中用来显示ArrayAdapter中数据的那个TextView

  public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

  public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)//建议使用这个,直接给ArrayAdapter填充了数据。

  方法:

  这个方法能够使用数组xml文件中配置的数据来创建一个ArrayAdapter,这个数组中的内容如何获得,通过this.getResources().getTextArray(id)方法获得。

  自定义数组xml文件的标识id号,也就是ArrayAdapter要绑定到ListVIew中的数据

  用于显示数组数据的布局文件的id标识号(注意:该布局文件中只能有一个TextView,有多个就会报错,一般是 ClassCastException)


对于他们的继承与实现之间的关系,大家可以在上面的文章里面可以很清楚的看到,在此基础上我还要对其进行扩展。

不论是那种适配器模式,也不管是Listview也好还是gridview也好,对他们填充数据,都是分三步走。

第一:创建一个数据填充的对象,可以是ListView, GridView, Gallery。

1
listView=(ListView) findViewById(R.id.listview_simple);

第二步:创建一个数据填充器,可以是BaseAdapter、SimpleAdapter,也可以是与数据库相关联的CursorAdapter。

例如:SimpleAdapter可以使用系统封装好的,你也可以自己去继承一个Simpleadapter,来重写其中的方法。继承 simpleadapter的好处在于,你可以对listitem中每个单一的控件设置监听事件等等一系列操作。如果用的是系统封装好的就有点爱莫能助了。

直接使用系统封装的:

1
2
3
4
5
6
SimpleAdapter simpleAdapter = new SimpleAdapter(
this ,
data,
R.layout.simple_item,
new String[] { "name" , "info" },
new int [] { R.id.simple_name, R.id.simple_info });
重写系统的simpleadpter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class ListSimpleAdpter extends SimpleAdapter{
//要使用到的数据源
private List<map>< string , object = "" >> data= new ArrayList</ string ,></map><map>< string , object = "" >>();
//填充数据的资源文件
private int resource;
private String[] from;
private Context context;
public ListSimpleAdpter(Context context,
List</ string ,></map><map>< string , object = "" >> data, int resource, String[] from,
int [] to) {
super(context, data, resource, from, to);
this .context=context;
this .data=data;
this .resource=resource;
this .from=from;
}
//item的总行数
@Override
public int getCount() {
// TODO Auto-generated method stub
return data== null ?0:data.size();
}
//item对象
@Override
public Object getItem( int position) {
// TODO Auto-generated method stub
return position;
}
//item的id
@Override
public long getItemId( int position) {
// TODO Auto-generated method stub
return position;
}
//绘制每一个item
@Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = null ;
if (convertView== null )
{
convertView=LayoutInflater.from(context).inflate(resource, null );
holder= new Holder();
holder.imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
holder.title=(TextView) convertView.findViewById(R.id.listitem_title);
holder.content=(TextView) convertView.findViewById(R.id.listitem_content);
convertView.setTag(holder);
} else
{
holder=(Holder) convertView.getTag();
}
holder.imageView.setImageResource(Integer.parseInt(data. get (position). get (from[0]).toString()));
holder.title.setText(data. get (position). get (from[1]).toString());
holder.content.setText(data. get (position). get (from[2]).toString());
return convertView;
}
class Holder{
ImageView imageView;
TextView title;
TextView content;
}
}</ string ,></map>
这里对于 Adapter的优化,用的很古老的ViewHolder、ViewCache办法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = null ;
if (convertView== null )
{
convertView=LayoutInflater.from(context).inflate(resource, null );
holder= new Holder();
holder.imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
holder.title=(TextView) convertView.findViewById(R.id.listitem_title);
holder.content=(TextView) convertView.findViewById(R.id.listitem_content);
convertView.setTag(holder);
} else
{
holder=(Holder) convertView.getTag();
}
holder.imageView.setImageResource(Integer.parseInt(data. get (position). get (from[0]).toString()));
holder.title.setText(data. get (position). get (from[1]).toString());
holder.content.setText(data. get (position). get (from[2]).toString());
return convertView;
}
class Holder{
ImageView imageView;
TextView title;
TextView content;
}
或者使用HashMap做缓存的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HashMap<integer, view= "" > m = new HashMap<integer, view= "" >();
public View getView( int position, View view, ViewGroup parent) {
View convertView = m. get (position);
if (convertView != null ) {
return convertView;
} else {
  convertView=LayoutInflater.from(context).inflate(resource, null );
  ImageView imageView=(ImageView) convertView.findViewById(R.id.listitem_pic);
  TextView title=(TextView) convertView.findViewById(R.id.listitem_title);
  TextView content=(TextView) convertView.findViewById(R.id.listitem_content);
   m.put(position, convertView);
}
}</integer,></integer,>
第三步:将数据填充到对象中去
1
listView.setAdapter(simpleAdapter);

这样就完成了数据填充器的数据填充。还有ArrayAdapter, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter,
ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter.SimpleCursorTreeAdapter、
二级树相关的SimpleExpandableListAdapter、BaseExpandableListAdapter等等。

转载 http://www.itivy.com/android/archive/2011/6/21/android-adapter-summary.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值