I\'m trying to make a many-to-many mapping of contacts to groups.
For example, if I have:
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.
A complete answer will be: First the fetch the group cursor (same as the answer above)
Cursor groups_cursor= getContentResolver().query(
ContactsContract.Groups.CONTENT_URI,
new String[]{
ContactsContract.Groups._ID,
ContactsContract.Groups.TITLE
}, null, null, null
);
Store all the group_id and group_title in a groups HashMap using this code:
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);
}
}
Then using the above answer's data_cursor, fetch contacts_ids and their 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
);
Now using the dataCursor and the groups 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 );
}
}
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;
}