下面是实现代码:
05
public
class
MyListView3
extends
ListActivity {
10
public
void
onCreate(Bundle savedInstanceState) {
11
super
.onCreate(savedInstanceState);
13
SimpleAdapter adapter =
new
SimpleAdapter(
this
,getData(),R.layout.vlist,
14
new
String[]{
"title"
,
"info"
,
"img"
},
15
new
int
[]{R.id.title,R.id.info,R.id.img});
16
setListAdapter(adapter);
19
private
List<Map<String, Object>> getData() {
20
List<Map<String, Object>> list =
new
ArrayList<Map<String, Object>>();
22
Map<String, Object> map =
new
HashMap<String, Object>();
23
map.put(
"title"
,
"G1"
);
24
map.put(
"info"
,
"google 1"
);
25
map.put(
"img"
, R.drawable.i1);
28
map =
new
HashMap<String, Object>();
29
map.put(
"title"
,
"G2"
);
30
map.put(
"info"
,
"google 2"
);
31
map.put(
"img"
, R.drawable.i2);
34
map =
new
HashMap<String, Object>();
35
map.put(
"title"
,
"G3"
);
36
map.put(
"info"
,
"google 3"
);
37
map.put(
"img"
, R.drawable.i3);
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。
运行效果如下图:
本文转自:
http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html