Get a contact's groups?
<p>I'm trying to make a many-to-many mapping of contacts to groups.</p>

<p>For example, if I have:</p>

<ul>
<li>User 1, belongs to group 701, 702, 704</li>
<li>User 2, belongs to no groups</li>
<li>User 3, belongs to group 702</li>
</ul>

<p>I'm hoping to get a relation that looks like this:</p>

<pre><code>userID | groupID
1      | 701
1      | 702
1      | 704
3      | 702
</code></pre>

<p>I've tried this:</p>

<pre><code>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);
</code></pre>

<p>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.</p>

<p>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!).</p>

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

<p>Thanks!</p>
