【发布时间】:2012-03-06 21:34:57
【问题描述】:
我创建了一个自定义列表,其中获取了所有电话簿联系人,并在我自己的列表中以自定义视图显示它们。我将所有联系人(包括联系人 ID)保存在数组列表中。当我单击一个列表时,我希望它以默认的 android 方式打开该联系人的所有详细信息。请有人告诉我这是否可能。
我的代码如下,用于将联系人保存在我自己的列表中:
arraylist = new ArrayList<PhoneBookUserEntity>();
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
PhoneBookUserEntity user = new PhoneBookUserEntity();
// Pick out the ID, and the Display name of the
// contact from the current row of the cursor
user.setId(cursor.getString(cursor.getColumnIndex(BaseColumns._ID)));
user.setPhoneBookName(cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME)));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ user.getId(), null, null);
while (phones.moveToNext()) {
user.sePhoneNumber(phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
//}
// user.sePhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user.getId(), null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
user.setEmailAddress(emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
emails.close();
user.setImageURI(getPhotoUri(user.getId()));
arraylist.add(user);
// Do something with the values you have,
// such as print them out or add to a list
//System.out.println("Current contact on this iteration is : " + name);
// This is where we query for Emails, Addresses etc
// Add snippets below into here, depending on what you need
}
}
cursor.close();
【问题讨论】:
-
顺便说一句,您可以使用联系人选择器来执行类似的功能。查看tutorials-android.blogspot.in/2011/11/…
-
我的自定义列表中有联系人。当有人点击联系人并显示联系人详细信息时,我想打开默认视图。