Adapter(适配器)可以看作是数据和view之间的桥梁,把数据通过一定的形式组织起来显示在view界面上(往往和ListView配合使用),可以类比JAVA I/O中的适配器模式。下图说明了Data、Adapter、View三者的关系。
下面介绍三种常用的adapter:
1.ArrayAdapter
ArrayAdapter对应的Listview每一行只包含单行文字
public class TestArrayAdapterActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
String[] strings = {"a", "b", "c", "d"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
setListAdapter(arrayAdapter);
}
}
这里直接继承ListAcivity这个类,ListActivity中的方法ensureList()中可看出ListAcivity包含了自定义的布局文件,list_content_simple.xml中包含了一个ListView
private void ensureList() {
if (mList != null) {
return;
}
setContentView(com.android.internal.R.layout.list_content_simple);
}
这里用到了构造方法ArrayAdapter(Context context, int textViewResourceId, T[] objects),
参数说明:
context:是活动的activity,传this就可以。
textViewResourceId:布局文件的资源ID,在android.R.layout.simple_list_item_1.xml这个文件中定义了一个
TextView用于显示文本。
objects:是要在listView上显示的对象,对象内的每个元素分别显示在一行。setListAdapter(arrayAdapter)用于把数据映射到ListView上。
2.SimpleAdapter
SimpleAdapter扩展性比较好,ListView每行可以实现相对复杂的布局。
布局文件simple.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
</LinearLayout>
这个文件用于list每一行的布局。
TestSimpleAdapterActivity.java:
public class TestSimpleAdapterActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = {"txt","img"};
//to的值从from传递过来
int[] to = {R.id.txt, R.id.img};
//public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(), R.layout.simple, from, to);
setListAdapter(simpleAdapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list= new ArrayList<Map<String, Object>>(50);
//A List of Maps. Each entry in the List corresponds to one row in the list.
//The Maps contain the data for each row, and should include all the entries specified in "from"
Map<String, Object> map = new HashMap<String, Object>();
map.put("txt", "A");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("txt", "B");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("txt", "C");
map.put("img", R.drawable.ic_launcher);
list.add(map);
map = new HashMap<String, Object>();
map.put("txt", "D");
map.put("img", R.drawable.ic_launcher);
list.add(map);
return list;
}
}
这里创建SimpleAdapter用了构造函数 public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数说明:
data:存放map对象的list,list中的map对应listView中的一行数据,每个map的键对应一行中的一个值。API文档说明:A List of Maps.
Each entry in the List corresponds to one row in the list.The Maps contain the data for each row, and
should include all the entries specified in "from"
from: 存放map中键的名称的数组
resource:自定义的资源文件
to:存放资源文件中的控件ID的数组,ID必须是参数resource中的ID
我的理解是在执行SimpleAdapter的构造方法时,把from[]中元素的值做为键从map中取出对应的值然后放入to[]中对应的控件中显示,to[]和
from[]中元素的顺序要特别注意,文档上的解释是:The first N views in this list are given the values of the first N columns in the from parameter。
即from[]中的第一个数据对应的值放入to[]中第一个ID对应的控件中,依此类推。
3.SimpleCusorAdapter
SimpleCusorAdapter使用方法和SimpleAdapter类似,用于数据库查询结果和ListView的绑定,构造方法中的cursor参数是执行数据库查询后得到的游标对象。
注:用SimpleCusorAdapter时数据库中一定要有_id这个字段做主键,运行时android会检查这个字段,如果cursor指向的结果集中没有这个"_id"字段,
会报“找不到_id字段”的错误。
public void showListView() {
//执行query方法返回的游标
cursor = noteDB.getAllNotes();
//数组中存放表的字段名称,要与cursor指向记录的字段名一致
String[] from = new String[] {"title", "content"};
int[] to = new int[] {R.id.text1, R.id.created_date};
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.note_row, cursor, from, to);
setListAdapter(adapter);
}
参考资料:http://www.cnblogs.com/devinzhang/archive/2012/01/20/2328334.html