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

Android Adapter

  这些适配器中,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

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2021-09-11
  • 2021-08-24
  • 2021-06-21
  • 2022-12-23
  • 2021-11-29
猜你喜欢
  • 2021-08-12
  • 2022-02-15
  • 2022-02-02
  • 2022-02-11
  • 2022-01-12
  • 2021-06-27
相关资源
相似解决方案