使用SimpleCursorTreeAdapter和ExpandableListActivity完成联系人列表,点击后显示其号码。

public class AppMainAcitivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 查询联系人并获取其Cursor对象
Cursor groupCursor = managedQuery(
ContactsContract.Contacts.CONTENT_URI, new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME },
null,
null,
null);
// 保存取联系人ID的列位置
mGroupIdColumnIndex = groupCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
//设置 adapter
MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(
groupCursor,
this,
android.R.layout.simple_expandable_list_item_1,//组视图(联系人)
android.R.layout.simple_expandable_list_item_1,//子视图(电话号码)
new String[] { ContactsContract.Contacts.DISPLAY_NAME }, //显示联系人名字
new int[] { android.R.id.text1 },
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, //显示电话号码
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
}
/**
*
* 因为SimpleCursorTreeAdapter是抽象类,所以我们要继承它并完成最终的功能
*
*/
private class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
public MyExpandableListAdapter(Cursor cursor, Context context,
int groupLayout, int childLayout, String[] groupFrom,
int[] groupTo, String[] childrenFrom, int[] childrenTo) {
//别忘了调用父类的同等参数的构造函数
//这里用了八个参数,lastChildLayout没有设置;还有就是groupLayout整合了collapsedGroupLayout和expandedGroupLayout
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
/**
*
* 回调函数,主要实现当选中某个组视图时,要查询指定的子视图所需的信息。
* 注意:
* 1.原例子中的电话号码的常量均以不推荐使用,所以采用最新的常量替换之。
* 2.别忘了在AdroidManifest.xml中配置权限。
*
*/
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
//获取联系人ID
Long contactId = groupCursor.getLong(mGroupIdColumnIndex);
// 构造电话号码URI
Uri.Builder builder = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
.buildUpon();
Uri phoneNumbersUri = builder.build();
//开始查询,Android推荐我们是其自身的managedQuery()方法进行查询,也就是自动管理Cursor。
return managedQuery(
phoneNumbersUri,
mPhoneNumberProjection,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? ",
new String[] { String.valueOf(contactId) }, null);
}
}
/**
* 要查询联系人的电话号码的字段(也就是子视图的信息)
*/
private String mPhoneNumberProjection[] = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER, };
/**
* 获取联系人ID的列位置,一般都是第一个,也就是0
*/
private int mGroupIdColumnIndex;
}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.chris.scta" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AppMainAcitivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- 授权读取联系人信息 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> </manifest>