SimpleAdapter

SimpleAdapter是ArrayList和ListView的桥梁。这个ArrayList里边的每一项都是一个Map<String,?>类型。
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&lt;?%20extends%20java.util.Map&lt;java.lang.String,%20?&gt;&gt;,%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>&lt;?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>&lt;<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>,?&gt;&gt; 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;
item=newHashMap<String,Object>();
item.put(“姓名”,”张三”);
item.put(“性别”,”男”);
data.add(item);
item=newHashMap<String,Object>();
item.put(“姓名”,”李四”);
item.put(“性别”,”女”);
data.add(item);
//构造listview对象。
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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值