如何在 Android 中获取联系人的 groupId / GroupName?

发布于 2024-10-07 02:59:31 字数 140 浏览 1 评论 0原文

我有 Android 设备中的联系人列表。我想获取所有联系人的关联 groupId 和 GroupName。我一直在尝试使用 ContactsContract.Groups._ID 来获取 ID,但无法获取它。有人可以提供其他方式来获取联系人的 groupID 吗?

I am having list of contacts present in an Android device. I want to fetch the associated groupIds and GroupName of all the contacts. I have been trying to use ContactsContract.Groups._ID to get the ID, but I am not able to get it. Can someone provide me other way to get the groupID of contact?

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

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

发布评论

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

评论(1

薆情海 2024-10-14 02:59:31

我就是这样做的。您可能可以通过不执行两个查询来解决问题并找到更快的解决方案。

这个想法是使用GroupMembership.GROUP_ROW_ID从数据表中获取组的行ID。当您拥有行 ID 时,您可以使用它来查询组表以获取组的名称(标题)。

通常,Groups.TITLE 并不是一个好名字,您可能必须对其进行格式化或四处搜索以找到更好的名称。

这是获取联系人 ID 的代码:

public long getGroupIdFor(Long contactId){
    Uri uri = Data.CONTENT_URI;
    String where = String.format(
            "%s = ? AND %s = ?",
            Data.MIMETYPE,
            GroupMembership.CONTACT_ID);

    String[] whereParams = new String[] {
               GroupMembership.CONTENT_ITEM_TYPE,
               Long.toString(contactId),
    };

    String[] selectColumns = new String[]{
            GroupMembership.GROUP_ROW_ID,
    };


    Cursor groupIdCursor = mContext.getContentResolver().query(
            uri, 
            selectColumns, 
            where, 
            whereParams, 
            null);
    try{
        if (groupIdCursor.moveToFirst()) {
            return groupIdCursor.getLong(0);
        }
        return Long.MIN_VALUE; // Has no group ...
    }finally{
        groupIdCursor.close();
    }
}

这是获取群组标题的代码:

public String getGroupNameFor(long groupId){
    Uri uri = Groups.CONTENT_URI;
    String where = String.format("%s = ?", Groups._ID);
    String[] whereParams = new String[]{Long.toString(groupId)};
    String[] selectColumns = {Groups.TITLE};
    Cursor c = mContext.getContentResolver().query(
            uri, 
            selectColumns,
            where, 
            whereParams, 
            null);

    try{
        if (c.moveToFirst()){
            return c.getString(0);  
        }
        return null;
    }finally{
        c.close();
    }
}

This is how I do it. You can probably mess around and find a faster solution by not doing two queries.

The idea is to get the row id of the group from the Data table using GroupMembership.GROUP_ROW_ID. When you have the row id you use that to query the Groups table to get the name (Title) of the group.

Often the Groups.TITLE isn't that good a name and you probably will have to format it or search around to find anything better.

Here is the code to get contact id:

public long getGroupIdFor(Long contactId){
    Uri uri = Data.CONTENT_URI;
    String where = String.format(
            "%s = ? AND %s = ?",
            Data.MIMETYPE,
            GroupMembership.CONTACT_ID);

    String[] whereParams = new String[] {
               GroupMembership.CONTENT_ITEM_TYPE,
               Long.toString(contactId),
    };

    String[] selectColumns = new String[]{
            GroupMembership.GROUP_ROW_ID,
    };


    Cursor groupIdCursor = mContext.getContentResolver().query(
            uri, 
            selectColumns, 
            where, 
            whereParams, 
            null);
    try{
        if (groupIdCursor.moveToFirst()) {
            return groupIdCursor.getLong(0);
        }
        return Long.MIN_VALUE; // Has no group ...
    }finally{
        groupIdCursor.close();
    }
}

And here is the code to get the Title of the group:

public String getGroupNameFor(long groupId){
    Uri uri = Groups.CONTENT_URI;
    String where = String.format("%s = ?", Groups._ID);
    String[] whereParams = new String[]{Long.toString(groupId)};
    String[] selectColumns = {Groups.TITLE};
    Cursor c = mContext.getContentResolver().query(
            uri, 
            selectColumns,
            where, 
            whereParams, 
            null);

    try{
        if (c.moveToFirst()){
            return c.getString(0);  
        }
        return null;
    }finally{
        c.close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文