android-listview控件

 

第一:修改xml文件

<ListView
android:id="@id/android:list"  //必须这么写,不然后报错
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

第二:设置适配器

extends ListActivity //修改继承的父类

SimpleAdapter listadapter = new SimpleAdapter(this, getdate(), R.layout.vlist, new String[]{"title","info"}, new int[]{R.id.textview1,R.id.textview2});
setListAdapter(listadapter);

 

第三:添加数据

private List<HashMap<String,Object>> getdate() {
// TODO Auto-generated method stub
ArrayList<HashMap<String,Object>> list= new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map1 = new HashMap<String,Object>();
HashMap<String,Object> map2 = new HashMap<String,Object>();
map1.put("title", "aaaa");
map1.put("info", "bbb");
map2.put("title", "cccc");
map2.put("info", "dddd");
list.add(map1);
list.add(map2);
return list;
}

第四:设置监听器

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(0==id){
System.out.println("0");

}
else if(1==id)
{
System.out.println("1");
}

}

你可能感兴趣的:(ListView)