我正在为自定义偏好编写contactpickerdialog。
取决于偏好,ContactPickerDialog应显示至少一个电话号码的所有联系人或至少有一个电子邮件地址的所有联系人。不应显示两次联系,无论与此联系人连接了多少个电话数或电子邮件地址。
我在与至少一个电话数据的所有联系人中都没有问题,因为我可以在were子中使用 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) { }
}
任何帮助都非常感谢
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);