android2.1取得通讯录联系人名字和电话号码

android2.0以后取得通讯录联系人使用的URI有所变化,改为ContactsContract类,以前的Contacts类已经不推荐使用了。

取得联系人的电话号码是需要先取得_ID, 根据id取得电话号码,并且电话号码存在多个的情况,需要考虑。

具体代码如下:

1. Activity类

public class AndroidTest extends Activity { private static final String TAG = "AndroidTest"; private ListView lv = null; private static final String COLUMN_ID = "No.";// INTEGER PRIMARY KEY private static final String COLUMN_NAME = "name";// TEXT private static final String COLUMN_NUMBER = "number";// INTEGER @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView) this.findViewById(R.id.ListView01); // 取得ContentResolver对象 ContentResolver cr = getContentResolver(); // 取得通讯录的光标 String orderBy = PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, orderBy); // 遍历通讯录 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); for(int i=0; i<cursor.getCount() ;i++) { HashMap<String, Object> map = new HashMap<String, Object>(); cursor.moveToPosition(i); // No. map.put(COLUMN_ID, i + 1); // 取得联系人名字 int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); String name = cursor.getString(nameFieldColumnIndex); map.put(COLUMN_NAME, name); // 取得联系人ID String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); String number = ""; // 取得电话号码(可能存在多个号码) for(int j = 0; j < phone.getCount(); j++) { phone.moveToPosition(j); String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (j > 0) { number += " , "; } number += strPhoneNumber; } map.put(COLUMN_NUMBER, number); Log.d(TAG, "number = " + number); phone.close(); listItem.add(map); } cursor.close(); // 生成适配器的Item和动态数组对应的元素 SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源 R.layout.list_item,// ListItem的XML实现 // 动态数组与ListItem对应的子项 new String[] { COLUMN_ID, COLUMN_NAME, COLUMN_NUMBER }, new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 }); lv.setAdapter(listItemAdapter); } }

2. main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background_color" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FF4169E1" android:text="通讯录:" /> <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:listSelector="@drawable/list_selector_color" android:divider="#808080" android:dividerHeight="1px" android:headerDividersEnabled="true" > </ListView> </LinearLayout>

3. list_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dip" android:paddingBottom="5dip" android:paddingLeft="5dip" android:gravity="center_vertical" > <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="#FF808080" android:text="" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="2dip" android:paddingBottom="2dip" android:paddingLeft="5dip" > <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FF9400D3" android:textSize="20px" android:text="" /> <TextView android:id="@+id/TextView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FF8A2BE2" android:text="" /> </LinearLayout> </LinearLayout>

4. background_color.xml用于设置layout背景

<?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FF87CEFA" android:endColor="#FF474747" android:angle="270.0" android:centerY="0.3" android:centerColor="#FFB0E0E6" /> </shape>

5. list_selector_color.xml用于设置list item被选中时的颜色

<?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FF90EE90" android:endColor="#FF98FB98" android:angle="270.0" android:centerY="0.3" android:centerColor="#FF00FF7F" /> </shape>

画面效果:

预览图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值