我正在为自定义首选项编写一个ContactPickerDialog。
根据首选项,ContactPickerDialog应显示至少有一个电话号码的所有联系人或具有至少一个电子邮件地址的所有联系人。无论有多少电话号码或电子邮件地址与此联系人连接,都不应显示两次联系。
我可以使用至少一个电话号码获取所有联系人,因为我可以在where子句中使用HAS_PHONE_NUMBER
。但我不知道如果联系人至少有一个电子邮件地址,如果他们有多个电子邮件地址,如果没有多次联系,可能会多次联系。
这是我的代码:
public void LoadContactsWithTarget(Context context, PickContactDialog.PickTarget pickTarget)
{
try
{
String[] projection = new String[]
{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.PHOTO_URI,
ContactsContract.Contacts._ID
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
Uri uri = null;
String where = "";
if (pickTarget == PickTarget.PHONE_NR)
{
// works great, I only get one contact, no matter how many phone-numbers are connected with this contact
uri = ContactsContract.Contacts.CONTENT_URI;
where = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1";
}
else if (pickTarget == PickTarget.EMAIL)
{
// I get one contact/entry for each eMail-address that is connected with the contact but I only want to get one contact, no matter how many eMail-addresse are connected with the contact
uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
where = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
}
Cursor cursor = context.getContentResolver().query(uri, projection, where, null, sortOrder);
if (cursor.moveToFirst())
{
int iNameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int iThumbnailColumn = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI);
int iImageColumn = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI);
int iIDColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
while (!cursor.isAfterLast())
{
Contact contact = new Contact(cursor.getInt(iIDColumn),
cursor.getString(iNameColumn),
cursor.getString(iThumbnailColumn),
cursor.getString(iImageColumn));
_lstContacts.add(contact);
cursor.moveToNext();
}
}
cursor.close();
}
catch(Exception e) { }
}
非常感谢任何帮助
答案 0 :(得分:0)
cursorContacts = contentResolver.query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Data.MIMETYPE,
ContactsContract.Contacts.Data.DATA1},
"("+ContactsContract.RawContacts.Data.MIMETYPE + "=?"
+" OR "+ContactsContract.RawContacts.Data.MIMETYPE + "=?)"
+" AND "+ContactsContract.Data.CONTACT_ID
+" IN (SELECT "+ContactsContract.Data.CONTACT_ID
+" FROM view_data"
+" WHERE "+ContactsContract.Data.MIMETYPE+"=?"
+" AND "+ContactsContract.Contacts.Data.DATA1+" NOT NULL"
+" AND "+ContactsContract.Contacts.Data.DATA1+" NOT LIKE ''"
+")"
,
new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
},
ContactsContract.Contacts.DISPLAY_NAME);