12

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!

4

3 回答 3

14
    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
    );

通过使用它dataCursor,您将获得联系人数据库中所有联系人的和contact_idgroup_id

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

通过使用它groupCursor,您将获得联系人数据库中所有组的group_idgroup_title

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

于 2012-12-31T07:23:07.927 回答
2

一个完整的答案将是:首先获取组游标(与上面的答案相同)

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

使用以下代码将所有 group_id 和 group_title 存储在组 HashMap 中:

 if(groups_cursor!=null){
        while(groups_cursor.moveToNext()){
            String group_title = groups_cursor.getString(1);
            String id = groups_cursor.getString(0);
            groups.put(id, group_title);
        }
    }

然后使用上述答案的data_cursor,获取contacts_ids 和他们的group_ids。

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
);

现在使用 dataCursor 和组 HashMap。

    if(dataCursor!=null){
            while(dataCursor.moveToNext()){
                String id  = dataCursor.getString(0);
                String group_id= dataCursor.getString(1);
                String groupTitle = groups.get(group_id);
                Log.d(TAG, "groupTitle : " + groupTitle + " contact_id: " + id );
           }
  }
于 2015-08-27T11:01:24.057 回答
0
 public static HashMap<String, String> getContactsForGroup(String groupID, Activity activity){
    Cursor dataCursor = activity.getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            new String[]{                                                       // PROJECTION
                    ContactsContract.Data.CONTACT_ID,
                    ContactsContract.Data.DISPLAY_NAME,         // contact name
                    ContactsContract.Data.DATA1                 // group
            },
            ContactsContract.Data.MIMETYPE + " = ? " + "AND " +                 // SELECTION
            ContactsContract.Data.DATA1 +    " = ? ",           // set groupID
            new String[]{                                                       // SELECTION_ARGS
                    ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                    groupID
            },
            null
    );

    dataCursor.moveToFirst();
    HashMap<String, String> map = new HashMap<>();
    while (dataCursor.moveToNext()) //
    {
        String s0 = dataCursor.getString(0);                //contact_id
        String s1 = dataCursor.getString(1);                //contact_name
        String s2 = dataCursor.getString(2);                //group_id
        Log.d("tag", "contact_id: " + s0 + "  contact: " + s1 + "   groupID: "+ s2);
        map.put(s0, s1);
    }
    return map;
}
于 2017-06-13T12:10:46.660 回答