检索特定联系人的组

发布于 2024-11-09 12:48:57 字数 1012 浏览 0 评论 0原文

我想检索联系方式及其所属的组。我得到了列出手机中所有联系人组的代码。

Cursor groupC = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI, null, null, null, null); 

while (groupC.moveToNext()) { 
    String groupid =
        groupC.getString(groupC.getColumnIndex(ContactsContract.Groups._ID));
    Log.e("myTag", groupid); 
    String grouptitle =
        groupC .getString(groupC.getColumnIndex(ContactsContract.Groups.TITLE));
    Log.e("myTag", grouptitle);
}
groupC.close();

然后我尝试使用其 id 查询特定联系人,但它始终显示 There is no such columns...

Cursor groupC = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI,
    null,
    ContactsContract.Contacts._ID+"= ?",
    new String[]{id},
    null);

其中 id 是

Cursor cur = cr.query(
    ContactsContract.Contacts.CONTENT_URI,
    null,
    null,
    null,
    null);
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

如何使用特定联系人 ID 查询组?

I want to retrieve the contact details along with the group which it belongs to. I got the code to list all the contact groups in the phone.

Cursor groupC = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI, null, null, null, null); 

while (groupC.moveToNext()) { 
    String groupid =
        groupC.getString(groupC.getColumnIndex(ContactsContract.Groups._ID));
    Log.e("myTag", groupid); 
    String grouptitle =
        groupC .getString(groupC.getColumnIndex(ContactsContract.Groups.TITLE));
    Log.e("myTag", grouptitle);
}
groupC.close();

Then I tried to query for a particular contact by using its id but it always shows There is no such column....

Cursor groupC = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI,
    null,
    ContactsContract.Contacts._ID+"= ?",
    new String[]{id},
    null);

where id is

Cursor cur = cr.query(
    ContactsContract.Contacts.CONTENT_URI,
    null,
    null,
    null,
    null);
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

How to query the group using a particular contact id?

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

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

发布评论

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

评论(1

吐个泡泡 2024-11-16 12:48:57

我找到了答案。我们应该传递原始联系人 ID 和正确的 MIME 类型。

  String where = ContactsContract.Data.RAW_CONTACT_ID
            + "="
            + Integer.parseInt(id)
            + " AND "
            + ContactsContract.Data.MIMETYPE
            + "='"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            + "'";

    Cursor cursor = ctx
            .getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI, null, where, null,
                    null);
    startManagingCursor(cursor);
    Log.e("Count is:", ""+ cursor.getCount());
    while (cursor.moveToNext()) {
        groupid = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
        Log.e("groupid", groupid);
        builder.append(groupid);

    }String where = ContactsContract.Data.RAW_CONTACT_ID
            + "="
            + Integer.parseInt(id)
            + " AND "
            + ContactsContract.Data.MIMETYPE
            + "='"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            + "'";

    Cursor cursor = ctx
            .getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI, null, where, null,
                    null);
    startManagingCursor(cursor);
    Log.e("Count is:", ""+ cursor.getCount());
    while (cursor.moveToNext()) {
        groupid = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
        Log.e("groupid", groupid);
        break;
    }

联系人可能位于多个组中,此处仅检索其第一组。

我认为这可能对某人有用......

I found the answer.we should pass the raw contact-id and the correct mime type.

  String where = ContactsContract.Data.RAW_CONTACT_ID
            + "="
            + Integer.parseInt(id)
            + " AND "
            + ContactsContract.Data.MIMETYPE
            + "='"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            + "'";

    Cursor cursor = ctx
            .getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI, null, where, null,
                    null);
    startManagingCursor(cursor);
    Log.e("Count is:", ""+ cursor.getCount());
    while (cursor.moveToNext()) {
        groupid = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
        Log.e("groupid", groupid);
        builder.append(groupid);

    }String where = ContactsContract.Data.RAW_CONTACT_ID
            + "="
            + Integer.parseInt(id)
            + " AND "
            + ContactsContract.Data.MIMETYPE
            + "='"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            + "'";

    Cursor cursor = ctx
            .getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI, null, where, null,
                    null);
    startManagingCursor(cursor);
    Log.e("Count is:", ""+ cursor.getCount());
    while (cursor.moveToNext()) {
        groupid = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
        Log.e("groupid", groupid);
        break;
    }

A contact may be in more than one group,here it retrivr its first group pnly.

I think this may be useful to somebody...

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