final ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 10; i++) {
HashMap<String, Object> user = new HashMap<String, Object>();
user.put("img", R.drawable.user);
user.put("username", "姓名(" + i+")");
user.put("age", (20 + i) + "");
users.add(user);
}
SimpleAdapter saImageItems = new SimpleAdapter(this,
users,// 数据来源
R.layout.user,//每一个user xml 相当ListView的一个组件
new String[] { "img", "username", "age" },
// 分别对应view 的id
new int[] { R.id.img, R.id.name, R.id.age });
// 获取listview
ListView myListView =((ListView) findViewById(R.id.users));
myListView.setAdapter(saImageItems);
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
String aa=(String) users.get(arg2).get("username");
Toast.makeText(ListViewTest.this, "你点击了第"+arg2+"项的"+aa,Toast.LENGTH_SHORT).show();
}
});