在xml里面定义一个ListView
<ListView
android:id="@+id/listview"android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
定义ListView每个item的Layout
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="5dip"
<ImageView android:id="@+id/cell_image" android:paddingTop="12dip"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/cell_name" android:layout_height="wrap_content"
android:textSize="20dip" android:layout_width="fill_parent"
android:textColor="#FF0F0FF0" />
<TextView android:id="@+id/cell_number" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_below="@+id/cell_name" />
</RelativeLayout>
Activity里
list = (ListView)findViewById(R.id.listview);
arraylist = new ArrayList<HashMap<String,Object>>();
for(int i = 0; i < 20; i ++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("cell_image",R.drawable.accept_button_hover);
map.put("cell_name","name_" + i);
map.put("cell_number","number_" + i);
arraylist.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, arraylist, R.layout.listcell, new String[]{"cell_name","cell_number","cell_image"},new int[]{R.id.cell_name,R.id.cell_number,R.id.cell_image});
list.setAdapter(adapter);
//添加点击
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
HashMap<String, Object> map = arraylist.get(position);
System.out.println(map.get("cell_name"));
}
});