Android ListView实现通讯录的例子 2011-9-10 16:40:01 上传 下载附件 (167.75 KB) 实现过程。 第一步:布局 list_items.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout android:id="@+id/widget35" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="5px" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#f8f8f8" > <TableRow android:id="@+id/widget36" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" > <FrameLayout android:id="@+id/widget39" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/head0" /> <AbsoluteLayout android:id="@+id/widget44" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:id="@+id/PhoneType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="45px" android:layout_y="15px" android:src="@drawable/ra11" /> </AbsoluteLayout> </FrameLayout> <TableLayout android:id="@+id/widget49" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5px" android:paddingTop="3px" android:paddingRight="10px" xmlns:android="http://schemas.android.com/apk/res/android" > <TableRow android:id="@+id/widget50" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" > <TextView android:id="@+id/ItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18px" android:textColor="#1d1d1d" android:text="TextView" /> </TableRow> <TableRow android:id="@+id/widget51" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingRight="10px" android:orientation="horizontal" > <TextView android:id="@+id/ItemPhoneNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14px" android:textColor="#8e8e8e" android:text="TextView" /> <TextView android:id="@+id/ItemCallDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12px" android:paddingLeft="10px" android:textColor="#8e8e8e" android:text="TextView" /> </TableRow> </TableLayout> <ImageView android:id="@+id/Call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_marginTop="10px" android:paddingLeft="10px" android:paddingRight="10px" android:src="@drawable/phone" /> </TableRow> </TableLayout> </LinearLayout> 复制代码 第二部:Activity package com.android.lst; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; /** * 用ListView实现通讯录的例子 * @author touchAndroid *http://bbs.droidstouch.com */ public class Test extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView list = (ListView)this.findViewById(R.id.myListView); this.setTitle("通话记录"); //参数一:context, //参数二:数据源 //参数三:布局文件 //参数4:数据源中Map中的Key //参数5:参数4中Map中的Key的值显示在哪个控件上 SimpleAdapter listItemAdapter = new SimpleAdapter(this, initData(), R.layout.list_items, new String[] {"ItemImage","PhoneType","ItemName","ItemPhoneNum","ItemCallDate"}, new int[] {R.id.ItemImage,R.id.PhoneType,R.id.ItemName,R.id.ItemPhoneNum,R.id.ItemCallDate} ); list.setAdapter(listItemAdapter); } /** * 构造数据源 * @return */ public ArrayList<HashMap<String,Object>> initData() { ArrayList<HashMap<String,Object>> listItem = new ArrayList<HashMap<String,Object>>(); for(int i = 0; i < 10; i++) { HashMap<String,Object> map = new HashMap<String,Object>(); try { map.put("ItemImage", R.drawable.class.getField("head"+i).get(this)); } catch (Exception e) { map.put("ItemImage", R.drawable.head); } try { map.put("PhoneType", R.drawable.class.getField("ra1"+ ((i%2==0)?1:2)).get(this)); } catch (Exception e) { map.put("PhoneType", R.drawable.ra11); } map.put("ItemName", "touchAndroid" + i); map.put("ItemPhoneNum", "13800138000"); map.put("ItemCallDate", "7天前"); listItem.add(map); } return listItem; } } 复制代码