Get list of contacts belonging to a specific group

user672740 picture user672740 · Mar 23, 2011 · Viewed 11.7k times · Source

Does anybody know how to get a list of contacts belonging to a 1 specific group in Android?

I need something like this:

Select * from contacts where group_id = "1234"

I am able to get a list of all contacts OR all groups by using something like this:

Cursor groupCursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
final ArrayList<String> contacts = new ArrayList<String>();

while(groupCursor.moveToNext()) {
    String name = groupCursor.getString(groupCursor.getColumnIndex(ContactsContract.Constacts.DisplayName    ));
    contacts.add(name);
}

Answer

DMcP89 picture DMcP89 · Feb 10, 2012

this is what i use and it works fine for me

Uri groupURI = ContactsContract.Data.CONTENT_URI;


    String[] projection = new String[]{
     ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID ,
     ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID};

    Cursor c = managedQuery(groupURI,
    projection,
    ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"="+groupID,
    null,null);

this requires you to have the Group id already and that can be found by querying ContactsContract.Groups