如何搜索没有群组的联系人

发布于 2024-12-29 13:52:47 字数 170 浏览 3 评论 0原文

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

I have a question about android contacts. I want to query all contacts have not assigned to any group, but I am not sure about the search condition, I mean, sometimes, the GroupMembership.GROUP_ROW_ID is null If the contacts have no group, but sometimes the GroupMembership.GROUP_ROW_ID is -1. Can anyone tell me how to search all contacts have no group?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2025-01-05 13:52:47

这有点粗糙,但对我有用。我找不到一种无需执行两次查询即可在任何组中查找联系人的简单方法。

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

This is a little crude, but it works for me. I couldn't see an easy way of finding contacts in no group without doing two queries.

It's likely that you can optimise the query to remove duplicates, thus removing the need for the 'foundIds' set.

    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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文