Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of contacts belonging to a specific group

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);
}
like image 815
user672740 Avatar asked Mar 23 '11 09:03

user672740


3 Answers

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

like image 86
DMcP89 Avatar answered Nov 02 '22 00:11

DMcP89


public void getSampleContactList(int groupID) {

        contactList = new ArrayList<ConatctData>();
        Uri groupURI = ContactsContract.Data.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID };

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

        while (c.moveToNext()) {
            String id = c
                    .getString(c
                            .getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
            Cursor pCur = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[] { id }, null);

            while (pCur.moveToNext()) {
                ConatctData data = new ConatctData();
                data.name = pCur
                        .getString(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                data.phone = pCur
                        .getString(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                contactList.add(data);
            }

            pCur.close();

        }
    }

    class ConatctData {
        String phone, name;
    }

Hope this will help you to find contact According to group id.

like image 39
Brajendra Pandey Avatar answered Nov 01 '22 23:11

Brajendra Pandey


    void getlistofcontacts( long groupId ) {
    String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID };

    Cursor groupCursor = getContentResolver().query(
            Data.CONTENT_URI,
            cProjection,
            CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
                    + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
                    + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
            new String[] { String.valueOf(groupId) }, null);
    if (groupCursor != null && groupCursor.moveToFirst())
    {
        //Toast.makeText(this,"if",1000).show();
        do
        {

            int nameCoumnIndex = groupCursor.getColumnIndex(Phone.DISPLAY_NAME);

            String name = groupCursor.getString(nameCoumnIndex);

            long contactId = groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID));

            Cursor numberCursor = getContentResolver().query(Phone.CONTENT_URI,
                    new String[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null);

            if (numberCursor.moveToFirst())
            {
                int numberColumnIndex = numberCursor.getColumnIndex(Phone.NUMBER);
                do
                {
                    String phoneNumber = numberCursor.getString(numberColumnIndex);

                    Toast.makeText(this,name+phoneNumber,1000).show();
                } while (numberCursor.moveToNext());
                numberCursor.close();
            }
            else
            {
                Toast.makeText(this,"no contact are there",1000).show();
            }
        } while (groupCursor.moveToNext());
        groupCursor.close();
    }
    else
    {
        Toast.makeText(this,"no such group exists",1000).show();
    }
}

and call this function from where you want

like image 21
vineeth Avatar answered Nov 02 '22 01:11

vineeth