【发布时间】:2012-02-21 11:44:36
【问题描述】:
我有一个关于 android 联系人的问题。我想查询所有未分配到任何组的联系人,但我不确定搜索条件,我的意思是,有时 GroupMembership.GROUP_ROW_ID 为 null 如果联系人没有组,但有时 GroupMembership.GROUP_ROW_ID 为-1 .谁能告诉我如何搜索所有没有组的联系人?
【问题讨论】:
标签: android contacts android-contacts
我有一个关于 android 联系人的问题。我想查询所有未分配到任何组的联系人,但我不确定搜索条件,我的意思是,有时 GroupMembership.GROUP_ROW_ID 为 null 如果联系人没有组,但有时 GroupMembership.GROUP_ROW_ID 为-1 .谁能告诉我如何搜索所有没有组的联系人?
【问题讨论】:
标签: android contacts android-contacts
这有点粗略,但对我有用。如果不进行两次查询,我看不到在任何组中查找联系人的简单方法。
您可能可以优化查询以删除重复项,从而消除对“foundIds”集的需要。
private Cursor getNoGroupsCursor() {
Set<String> groupIds = new HashSet<String>();
Set<String> foundIds = new HashSet<String>();
// Get the group membership
Cursor gmCursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data.CONTACT_ID, Data.MIMETYPE, Data.DATA1 },
Data.MIMETYPE + " = '" + GroupMembership.CONTENT_ITEM_TYPE
+ "'", null, null);
while (gmCursor.moveToNext()) {
String groupTitle = gmCursor.getString(2);
// We need to pre-parse the group ids to find all the ones that
// have a group
if (groupTitle != null && !"-1".equals(groupTitle)) {
groupIds.add(gmCursor.getString(0));
}
}
gmCursor.close();
// Get the contacts
gmCursor = getContentResolver().query(Data.CONTENT_URI,
new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME }, null,
null, "2 ASC");
// We want a projected matrix cursor that contains the contact ID and
// group name, rather than the group ID
MatrixCursor mc = new MatrixCursor(PROJECTION_GROUPS);
while (gmCursor.moveToNext()) {
String id = gmCursor.getString(0);
if (!foundIds.contains(id) && !groupIds.contains(id)) {
mc.addRow(new Object[] { gmCursor.getString(0),
gmCursor.getString(1), null, null });
foundIds.add(id);
}
}
gmCursor.close();
return mc;
}
【讨论】: