Skip to content Skip to sidebar Skip to footer

Getting Contacts From A Specified Group In Android

I'm trying to get contacts from a specific group of known id.i'm able to get group id and contact names in that group.but i'm unable to get contact numbers.tried some solutions fro

Solution 1:

Try to use following code in your onListItemClick method:

longgroupId= id;
    String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID };

    CursorgroupCursor= getContentResolver().query(
            Data.CONTENT_URI,
            cProjection,
            CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
                    + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
                    + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
            newString[] { String.valueOf(groupId) }, null);
    if (groupCursor != null && groupCursor.moveToFirst())
    {
        do
        {

            intnameCoumnIndex= groupCursor.getColumnIndex(Phone.DISPLAY_NAME);

            Stringname= groupCursor.getString(nameCoumnIndex);

            longcontactId= groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID));

            CursornumberCursor= getContentResolver().query(Phone.CONTENT_URI,
                    newString[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null);

            if (numberCursor.moveToFirst())
            {
                intnumberColumnIndex= numberCursor.getColumnIndex(Phone.NUMBER);
                do
                {
                    StringphoneNumber= numberCursor.getString(numberColumnIndex);
                    Log.d("your tag", "contact " + name + ":" + phoneNumber);
                } while (numberCursor.moveToNext());
                numberCursor.close();
            }
        } while (groupCursor.moveToNext());
        groupCursor.close();
    }

You need to specify mimetype when querying for contacts, which belong to specified group. And because single contact can have multiple phone numbers, you may need to query all of them.

EDIT: To get all groups with at least one contact with phone number you can use following query:

ContentResolvercr= getContentResolver();
UrigroupsUri= ContactsContract.Groups.CONTENT_SUMMARY_URI;
Stringwhere="((" + ContactsContract.Groups.GROUP_VISIBLE + " = 1) AND (" 
                + ContactsContract.Groups.SUMMARY_WITH_PHONES + "!= 0))";
Cursorcursor= cr.query(groupsUri, null, where, null, null);

As for getting contacts with phone number from a specified group - I don't know simple solution.

Solution 2:

Here is the answwer to what you are asking for. Get all phone-numbers from specific group #name. This spagetti can surely be shorter, if you use some time on it, to optimize it a bit.

public ArrayList<String> getAllNumbersFromGroupId(String navn)
{
    Stringselection= ContactsContract.Groups.DELETED + "=? and " + ContactsContract.Groups.GROUP_VISIBLE + "=?";
    String[] selectionArgs = { "0", "1" };
    Cursorcursor= context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, selection, selectionArgs, null);
    cursor.moveToFirst();
    intlen= cursor.getCount();

    ArrayList<String> numbers = newArrayList<String>();
    for (inti=0; i < len; i++)
    {
        Stringtitle= cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE));
        Stringid= cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID));

        if (title.equals(navn))
        {
            String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID };

            CursorgroupCursor= context.getContentResolver().query(
                    Data.CONTENT_URI,
                    cProjection,
                    CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND "
                            + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='"
                            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
                    newString[] { String.valueOf(id) }, null);
            if (groupCursor != null && groupCursor.moveToFirst())
            {
                do
                {

                    intnameCoumnIndex= groupCursor.getColumnIndex(Phone.DISPLAY_NAME);

                    Stringname= groupCursor.getString(nameCoumnIndex);

                    longcontactId= groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID));

                    CursornumberCursor= context.getContentResolver().query(Phone.CONTENT_URI,
                            newString[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null);

                    if (numberCursor.moveToFirst())
                    {
                        intnumberColumnIndex= numberCursor.getColumnIndex(Phone.NUMBER);
                        do
                        {
                            StringphoneNumber= numberCursor.getString(numberColumnIndex);
                            numbers.add(phoneNumber);
                        } while (numberCursor.moveToNext());
                        numberCursor.close();
                    }
                } while (groupCursor.moveToNext());
                groupCursor.close();
            }
            break;
        }

        cursor.moveToNext();
    }
    cursor.close();

    return numbers;
}

Solution 3:

my solution

publicstaticHashMap<String, String> getContactsForGroup(String groupID, Activity activity){
    Cursor dataCursor = activity.getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            newString[]{                                                     // PROJECTIONContactsContract.Data.CONTACT_ID,
                    ContactsContract.Data.DISPLAY_NAME,         // contact nameContactsContract.Data.DATA1// group
            },
            ContactsContract.Data.MIMETYPE + " = ? " + "AND " +               // SELECTIONContactsContract.Data.DATA1 +    " = ? ",           // set groupIDnewString[]{                                                     // SELECTION_ARGSContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                    groupID
            },
            null);
    dataCursor.moveToFirst();
    HashMap<String, String> map = newHashMap<>();
    while (dataCursor.moveToNext()) //
    {
        String s0 = dataCursor.getString(0);                //contact_idString s1 = dataCursor.getString(1);                //contact_nameString s2 = dataCursor.getString(2);                //group_idLog.d("tag", "contact_id: " + s0 + "  contact: " + s1 + "   groupID: "+ s2);
        map.put(s0, s1);
    }
    return map;
}

Post a Comment for "Getting Contacts From A Specified Group In Android"