【问题标题】:How to search contacts have no group如何搜索联系人没有组
【发布时间】:2012-02-21 11:44:36
【问题描述】:

我有一个关于 android 联系人的问题。我想查询所有未分配到任何组的联系人,但我不确定搜索条件,我的意思是,有时 GroupMembership.GROUP_ROW_ID 为 null 如果联系人没有组,但有时 GroupMembership.GROUP_ROW_ID 为-1 .谁能告诉我如何搜索所有没有组的联系人?

【问题讨论】:

    标签: android contacts android-contacts


    【解决方案1】:

    这有点粗略,但对我有用。如果不进行两次查询,我看不到在任何组中查找联系人的简单方法。

    您可能可以优化查询以删除重复项,从而消除对“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;
    }
    

    【讨论】:

    • 是的,我知道 ID 是长而不是字符串。嗯。
    • 感谢您的回复,现在我只能用两步搜索来解决这个问题,也许我们可以找到更好的方法,比如搜索view_data视图。
    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多