我正在尝试提取联系人列表并附加与给定人员相关联的所有联系人。我遇到的问题是我的查询返回所有已通过电子邮件发送的联系人。我不想包括我通过电子邮件发送的所有联系人,但我只想要主要的联系人(比如内置的People应用程序中显示的内容)。
在下面的代码中,我试图通过使用ContactsContract.Contacts.CONTENT_URI查询限制返回的电子邮件来限制电子邮件,但这似乎不起作用。
非常感谢任何帮助。
private ArrayList<ContactFriend> allContacts;
private Cursor contactsCursor;
private Cursor emailCursor;
void load() {
getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, this);
getLoaderManager().initLoader(EMAILS_LOADER_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id == CONTACTS_LOADER_ID) {
String[] projection = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC";
return new CursorLoader(getActivity(),
ContactsContract.Contacts.CONTENT_URI,
projection,
null,
null,
sortOrder);
} else if (id == EMAILS_LOADER_ID) {
String[] projection = {
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
};
String sortOrder =
ContactsContract.CommonDataKinds.Email.CONTACT_ID + ", " +
ContactsContract.CommonDataKinds.Email.DATA + " ASC";
return new CursorLoader(getActivity(),
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
projection,
null,
null,
sortOrder);
} else {
// should never happen
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
int id = loader.getId();
if (id == CONTACTS_LOADER_ID) {
contactsCursor = cursor;
} else if (id == EMAILS_LOADER_ID) {
emailCursor = cursor;
}
if (contactsCursor != null && emailCursor != null) {
init();
}
}
void init() {
if (allContacts == null) {
allContacts = getAllContacts();
}
// do stuff with allContacts
}
private ArrayList<ContactFriend> getAllContacts() {
HashMap<Integer, ArrayList<String>> emails = getEmailMap();
ArrayList<ContactFriend> result = new ArrayList<ContactFriend>();
contactsCursor.moveToFirst();
do {
String name = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
String photo = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
int id = contactsCursor.getInt(contactsCursor.getColumnIndex(ContactsContract.Contacts._ID));
ArrayList<String> contactEmails = emails.get(id);
if (!contactEmails.isEmpty()) {
result.add(new ContactFriend(name, contactEmails, photo, id));
}
} while (contactsCursor.moveToNext());
return result;
}
private HashMap<Integer, ArrayList<String>> getEmailMap() {
HashMap<Integer, ArrayList<String>> result = new HashMap<Integer, ArrayList<String>>();
while (contactsCursor.moveToNext()) {
int id = contactsCursor.getInt(contactsCursor.getColumnIndex(ContactsContract.Contacts._ID));
result.put(id, new ArrayList<String>());
}
Set<Integer> contactIds = result.keySet();
while (emailCursor.moveToNext()) {
int id = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
if (contactIds.contains(id)) {
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
result.get(id).add(email);
}
}
return result;
}
答案 0 :(得分:4)
添加此项以防其他人遇到同样的问题。将ContactsContract.Contacts.IN_VISIBLE_GROUP添加到投影中,然后仅选择那些值等于1的条目,这样我就能找到我想要的东西。