先定义一个显示条目的xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginTop="10dip" android:text="姓名:" android:textColor="#000000" android:textSize="20dp" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:layout_marginRight="80dp" android:layout_marginTop="10dip" android:drawableLeft="@android:drawable/ic_menu_call" android:text="电话号" android:textColor="#000000" android:textSize="20dp" /> </RelativeLayout>
在来程序中实现以下逻辑
package com.wzw.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; public class SelectContactActivity extends Activity { private ListView list_select_contact; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_contact); list_select_contact=(ListView) findViewById(R.id.list_select_contact); List<Map<String, String>> data = getContactInfo(); list_select_contact.setAdapter(new SimpleAdapter(this, data , R.layout.contact_item, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone})); } /** * 获取所有联系人信息 * @return */ private List<Map<String, String>> getContactInfo() { //存放所有的联系人 List<Map<String, String>> list = new ArrayList<Map<String,String>>(); // 得到一个内容解析器 ContentResolver resolver = getContentResolver(); // raw_contacts uri Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri uriData = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String contact_id = cursor.getString(0); if (contact_id != null) { //具体的某一个联系人 Map<String, String> map = new HashMap<String, String>(); Cursor dataCursor = resolver.query(uriData, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contact_id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); System.out.println("data1=="+data1+"==mimetype=="+mimetype); if("vnd.android.cursor.item/name".equals(mimetype)){ //联系人的姓名 map.put("name", data1); }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){ //联系人的电话号码 map.put("phone", data1); } } list.add(map); dataCursor.close(); } } cursor.close(); return list; } }主要是运用内容提供者来操作数据库,比较常用的代码,方便以后copy