SimpleAdapter是ArrayList和ListView的桥梁。这个ArrayList里边的每一项都是一个Map<String,?>类型。
ArrayList当中的每一项Map对象都和ListView里边的每一项进行数据绑定一一对应。
SimpleAdapter的构造函数:
参数:
context:上下文。
data:基于Map的list。Data里边的每一项都和ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
resource:就是一个布局layout,可引用系统提供的,也可以自定义。
from:这是个名字数组,每个名字是为了在ArrayList数组的每一个item索引Map<String,Object>的Object用的。
to:里面是一个TextView数组。这些TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。
下面通过一个类子来加深理解:
listitem.xml文件:
<?xmlversion=”1.0″encoding=”utf-8″?>
<LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<TextViewandroid:id=” @+id/mview1 ″android:layout_width=”100px”
android:layout_height=”wrap_content”/>
<TextViewandroid:id=” @+id/mview2 ″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
</LinearLayout>
下面是activity文件的部分代码
ArrayList当中的每一项Map对象都和ListView里边的每一项进行数据绑定一一对应。
SimpleAdapter的构造函数:
<nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/SimpleAdapter.html#SimpleAdapter(android.content.Context,%20java.util.List<?%20extends%20java.util.Map<java.lang.String,%20?>>,%20int,%20java.lang.String%5B%5D,%20int%5B%5D)" style="color:rgb(0,102,153); line-height:21px; text-decoration:underline">SimpleAdapter</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Context</a>context,<a rel="nofollow" href="http://developer.android.com/reference/java/util/List.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">List</a><?extends<a rel="nofollow" href="http://developer.android.com/reference/java/util/Map.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Map</a><<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>,?>> data, int resource,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String[]</a>from, int[] to)</nobr>
Constructor
|
context:上下文。
data:基于Map的list。Data里边的每一项都和ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
resource:就是一个布局layout,可引用系统提供的,也可以自定义。
from:这是个名字数组,每个名字是为了在ArrayList数组的每一个item索引Map<String,Object>的Object用的。
to:里面是一个TextView数组。这些TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。
下面通过一个类子来加深理解:
listitem.xml文件:
<?xmlversion=”1.0″encoding=”utf-8″?>
<LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<TextViewandroid:id=” @+id/mview1 ″android:layout_width=”100px”
android:layout_height=”wrap_content”/>
<TextViewandroid:id=” @+id/mview2 ″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
</LinearLayout>
下面是activity文件的部分代码
//构造数据部分
List<Map<String,Object>>data=newArrayList<Map<String,Object>>();
Map<String,Object>item;
List<Map<String,Object>>data=newArrayList<Map<String,Object>>();
Map<String,Object>item;
item=newHashMap<String,Object>();
item.put(“姓名”,”张三”);
item.put(“性别”,”男”);
data.add(item);
item=newHashMap<String,Object>();
item.put(“姓名”,”李四”);
item.put(“性别”,”女”);
data.add(item);
item.put(“姓名”,”张三”);
item.put(“性别”,”男”);
data.add(item);
item=newHashMap<String,Object>();
item.put(“姓名”,”李四”);
item.put(“性别”,”女”);
data.add(item);
//构造listview对象。
ListViewlistview=newListView(this);
ListViewlistview=newListView(this);
/*构造一个适配器。
* 第三个参数是说明用的是自定义的布局R.layout.listtiem。
* 第四和第五个参数要一起理解:
*把我们添加数据时姓名那一列对应到R.id.TextView01这个TextView中,把性别对应到R.id.TextView02这个TextView中。
*如果把from改为newString[]{“姓名”,”姓名”},测试下就会明白。
*/
SimpleAdapter adapter=newSimpleAdapter(this,data,R.layout.listitem,newString[]{“姓名”,”性别”},newint[]{R.id. TextView01,R.id. TextView02});
listview.setAdapter( adapter);
setContentView(listview);
* 第三个参数是说明用的是自定义的布局R.layout.listtiem。
* 第四和第五个参数要一起理解:
*把我们添加数据时姓名那一列对应到R.id.TextView01这个TextView中,把性别对应到R.id.TextView02这个TextView中。
*如果把from改为newString[]{“姓名”,”姓名”},测试下就会明白。
*/
SimpleAdapter adapter=newSimpleAdapter(this,data,R.layout.listitem,newString[]{“姓名”,”性别”},newint[]{R.id. TextView01,R.id. TextView02});
listview.setAdapter( adapter);
setContentView(listview);