获取联系人的群体? [英] Get a contact's groups?

查看:124
本文介绍了获取联系人的群体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行接触的很多一对多映射到组。

例如,如果我有:

  • 用户1,属于组701,702,704
  • 用户2,不属于任何组
  • 用户3,所属组702

我希望能得到一个关系,看起来是这样的:

 用户ID | GROUPID
1 | 701
1 | 702
1 | 704
3 | 702
 

我已经试过这样:

 光标光标= contentResolver.query(ContactsContract.Data.CONTENT_URI,空,新的String [] {
    ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID,
    ContactsContract.CommonDataKinds.GroupMembership.GROUP_SOURCE_ID
},NULL,NULL,NULL);
 

但是,这完全不是那么回事。该GROUP_SOURCE_ID列返回奇怪的号码不属于任何组的ID。有时甚至返回0或负数。

我可以构建这样一个映射所经历的每个组,并找到所有联系人在该组中,但需要占用大量的查询,而我试图保持快速(显然,刚刚那几个疑问是相当慢!)。

谁能告诉我怎样才能得到这个联系人到组映射在一个查询?

谢谢!

解决方案

 光标dataCursor = getContentResolver()查询(
            ContactsContract.Data.CONTENT_URI,
            新的String [] {
                    ContactsContract.Data.CONTACT_ID,
                    ContactsContract.Data.DATA1
            },
            ContactsContract.Data.MIMETYPE +=?,
            新的String [] {} ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,空
    );
 

通过使用该 dataCursor 您将获得 CONTACT_ID GROUP_ID 。

 光标groupCursor = getContentResolver()查询(
            ContactsContract.Groups.CONTENT_URI,
            新的String [] {
                    ContactsContract.Groups._ID,
                    ContactsContract.Groups.TITLE
            },NULL,NULL,NULL
    );
 

通过使用该 groupCursor 您将获得 GROUP_ID group_title 。

所以,如果你想获得与相关联的所有用户组 CONTACT_ID 第一个获得 dataCursor 使用合适的select语句。使用 dataCursor ,你可以得到所有的 GROUP_ID 与此有关 CONTACT_ID 。现在,使用 groupCursor ,你可以获取与该特定联系人相关的所有组信息。

I'm trying to make a many-to-many mapping of contacts to groups.

For example, if I have:

  • User 1, belongs to group 701, 702, 704
  • User 2, belongs to no groups
  • User 3, belongs to group 702

I'm hoping to get a relation that looks like this:

userID | groupID
1      | 701
1      | 702
1      | 704
3      | 702

I've tried this:

Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, new String[] {
    ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID,
    ContactsContract.CommonDataKinds.GroupMembership.GROUP_SOURCE_ID
}, null, null, null);

But that doesn't quite work. The GROUP_SOURCE_ID column returns weird numbers that aren't the ID of any groups. Sometimes it even returns 0 or a negative number.

I could construct a mapping of this by going through each group, and finding all contacts in that group, but that would take a lot of queries, and I'm trying to stay fast (apparently, just those few queries are quite slow!).

Can anyone tell me how I can get this contacts-to-groups mapping in one query?

Thanks!

解决方案

    Cursor dataCursor = getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            new String[]{
                    ContactsContract.Data.CONTACT_ID,
                    ContactsContract.Data.DATA1
            },
            ContactsContract.Data.MIMETYPE + "=?",
            new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}, null
    );

By using this dataCursor you will get the contact_id and group_id of all contacts in the contact database.

    Cursor groupCursor = getContentResolver().query(
            ContactsContract.Groups.CONTENT_URI,
            new String[]{
                    ContactsContract.Groups._ID,
                    ContactsContract.Groups.TITLE
            }, null, null, null
    );

By using this groupCursor you will get the group_id and group_title of all groups in the contact database.

So if you want to get all groups associated with a contact_id the first get the dataCursor using suitable select statements. Using dataCursor you can get all the group_id associated with that contact_id. Now using groupCursor you can get the information about all groups associated with that specific contact.

这篇关于获取联系人的群体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆