Uri uri;
// email query
uri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI,
Uri.encode(mKeyword));
cursor = resolver.query(uri, EMAIL_SEARCH_PROJECTION, null, null, null);
if (cursor != null) {
try {
while (cursor.moveToNext()) {
long id = cursor.getLong(ContactSummary.CONTACT_SUMMARY_COL_ID);
if (!excludeIds.contains(id) && !contactIds.contains(id)) {
contactIds.add(id);
}
}
} finally {
cursor.close();
cursor = null;
}
}
// phone number start to query the phone only if the number
// looks like a phone
if (Telephony.isPossiblePhoneNum(mKeyword)) {
if (Config.DEBUG) {
Log.v(TAG, mKeyword + " is a number");
}
uri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
Uri.encode(mKeyword));
cursor = resolver.query(uri, PHONE_SEARCH_PROJECTION, null, null, null);
if (cursor != null) {
try {
while (cursor.moveToNext()) {
long id = cursor.getLong(ContactSummary.CONTACT_SUMMARY_COL_ID);
if (!excludeIds.contains(id) && !contactIds.contains(id)) {
contactIds.add(id);
}
}
} finally {
cursor.close();
cursor = null;
}
}
} private static final String[] EMAIL_SEARCH_PROJECTION = new String[] {
Email.CONTACT_ID
};
private static final String[] PHONE_SEARCH_PROJECTION = new String[] {
Phone.CONTACT_ID
};
// email query
uri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI,
Uri.encode(mKeyword));
cursor = resolver.query(uri, EMAIL_SEARCH_PROJECTION, null, null, null);
if (cursor != null) {
try {
while (cursor.moveToNext()) {
long id = cursor.getLong(ContactSummary.CONTACT_SUMMARY_COL_ID);
if (!excludeIds.contains(id) && !contactIds.contains(id)) {
contactIds.add(id);
}
}
} finally {
cursor.close();
cursor = null;
}
}
// phone number start to query the phone only if the number
// looks like a phone
if (Telephony.isPossiblePhoneNum(mKeyword)) {
if (Config.DEBUG) {
Log.v(TAG, mKeyword + " is a number");
}
uri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
Uri.encode(mKeyword));
cursor = resolver.query(uri, PHONE_SEARCH_PROJECTION, null, null, null);
if (cursor != null) {
try {
while (cursor.moveToNext()) {
long id = cursor.getLong(ContactSummary.CONTACT_SUMMARY_COL_ID);
if (!excludeIds.contains(id) && !contactIds.contains(id)) {
contactIds.add(id);
}
}
} finally {
cursor.close();
cursor = null;
}
}
} private static final String[] EMAIL_SEARCH_PROJECTION = new String[] {
Email.CONTACT_ID
};
private static final String[] PHONE_SEARCH_PROJECTION = new String[] {
Phone.CONTACT_ID
};
06
07 private ListView listView;
08 //private List<String> data = new ArrayList<String>();
09 @Override
10 public void onCreate(Bundle savedInstanceState){
11 super .onCreate(savedInstanceState);
12
13 listView = new ListView( this );
14
15 Cursor cursor = getContentResolver().query(People.CONTENT_URI, null , null , null , null );
16 startManagingCursor(cursor);
17
18 ListAdapter listAdapter = new SimpleCursorAdapter( this , android.R.layout.simple_expandable_list_item_1,
19 cursor,
20 new String[]{People.NAME},
21 new int []{android.R.id.text1});
22
23 listView.setAdapter(listAdapter);
24 setContentView(listView);
25 }
26
27
28 }
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。 startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。 SimpleCursorAdapter 构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的 int型数组。其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应id的组件上。上面的代码,将NAME列的数据一次映射到布局文 件的id为text1的组件上。注意:需要在AndroidManifest.xml中如权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>