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文件:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal” android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<TextView android:id=”
@+id/mview1
″ android:layout_width=”100px”
android:layout_height=”wrap_content” />
<TextView android:id=”
@+id/mview2
″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</LinearLayout>
下面是activity文件的部分代码
//构造数据部分
List<Map<String,
Object>>
data
=
new
ArrayList<Map<String,Object>>();
Map<String,Object>
item;
item
=
new
HashMap<String,Object>();
item.put(“姓名”,”张三”);
item.put(“性别”,”男”);
data.add(item);
item
=
new
HashMap<String,Object>();
item.put(“姓名”,”李四”);
item.put(“性别”,”女”);
data.add(item);
//构造listview对象。
ListView
listview=
new
ListView(this);
SimpleAdapter
adapter
=
new
SimpleAdapter(this,data,R.layout.listitem,new
String[]{“姓名”,”性别”},new
int[]{R.id.
TextView01,R.id.
TextView02});
listview.setAdapter(
adapter);
setContentView(listview);
ArrayList当中的每一项
SimpleAdapter的构造函数:
SimpleAdapter(
Context
Constructor
|
下面通过一个类子来加深理解:
listitem.xml文件:
下面是activity文件的部分代码