I'm trying to make a many-to-many mapping of contacts to groups.
For example, if I have:
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!
In Mail, on the Home tab, in the New group, click New Items, then click More Items, and then click Contact Group. In the Name box, type a name for the contact group. On the Contact Group tab, in the Members group, click Add Members, and then click either From Outlook Contacts or From Address Book.
Open Outlook on the web. In the left pane, next to Groups, select the + button. (If you don't see the + button, hover your mouse over the left pane.) Note: If you don't see Groups in the left pane, your organization may not have turned on Groups.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With