1.作用是ArrayList和 ListView的橋梁。這個ArrayList里邊的每一項都是一個Map<String,?>類型。
? ?? ?ArrayList當中的每一項 Map對象都和ListView里邊的每一項進行數據綁定一一對應。

2.SimpleAdapter的構造函數:

SimpleAdapter(Context??context, List<? extends Map<String, ?>>??data, int resource, String[]??from, int[] to)

 參數:
? ?? ?1.context:上下文。
? ?? ?2.data:基于Map的list。Data里邊的每一項都和 ListView里邊的每一項對應。Data里邊的每一項都是一個Map類型,這個Map類里邊包含了ListView每一行需要的數據。
? ?? ?3.resource :就是一個布局layout,可引用系統提供的,也可以自定義。
? ?? ?4.from:這是個名字數組,每個名字是為了在 ArrayList數組的每一個item索引Map<String,Object>的Object用的。即 map 中得key值
? ?? ?5.to:里面是一個TextView數組。這些 TextView是以id的形式來表示的。例如:Android.R.id.text1,這個text1在layout當中是可以索引的。

3.通過一個類子來理解下:

??? 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/TextView01″?android:layout_width=”100px”android:layout_height=”wrap_content”?/><TextView?android:id=”@+id/TextView02″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=?new?ListView(this);????//構造listview對象。SimpleAdapter?adapter?=?new?SimpleAdapter(this,data,R.layout.listitem,new?String[]{“姓名”,”性別”},new?int[]{R.id.TextView01,R.id.TextView02});????/*構造一個適配器。*??1,第三個參數是說明用的是自定義的布局R.layout.listtiem。*??2,第四和第五個參數一起理解:*???把我們添加數據時姓名那一列對應到R.id.TextView01這個TextView中,把性別對應到R.id.TextView02這個?TextView中。*???如果把from改為new?String[]{“姓名”,”姓名”},測試下就會明白。????*/listview.setAdapter(adapter);setContentView(listview);

復制代碼

?

?