getting contacts from a specified group in android

℡╲_俬逩灬. 提交于 2019-12-01 07:03:03

Try to use following code in your onListItemClick method:

long groupId = id;
    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())
    {
        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);
                    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:

ContentResolver cr = getContentResolver();
Uri groupsUri = ContactsContract.Groups.CONTENT_SUMMARY_URI;
String where = "((" + ContactsContract.Groups.GROUP_VISIBLE + " = 1) AND (" 
                + ContactsContract.Groups.SUMMARY_WITH_PHONES + "!= 0))";
Cursor cursor = cr.query(groupsUri, null, where, null, null);

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

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)
{
    String selection = ContactsContract.Groups.DELETED + "=? and " + ContactsContract.Groups.GROUP_VISIBLE + "=?";
    String[] selectionArgs = { "0", "1" };
    Cursor cursor = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, selection, selectionArgs, null);
    cursor.moveToFirst();
    int len = cursor.getCount();

    ArrayList<String> numbers = new ArrayList<String>();
    for (int i = 0; i < len; i++)
    {
        String title = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE));
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID));

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

            Cursor groupCursor = context.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(id) }, null);
            if (groupCursor != null && groupCursor.moveToFirst())
            {
                do
                {

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

                    String name = groupCursor.getString(nameCoumnIndex);

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

                    Cursor numberCursor = context.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);
                            numbers.add(phoneNumber);
                        } while (numberCursor.moveToNext());
                        numberCursor.close();
                    }
                } while (groupCursor.moveToNext());
                groupCursor.close();
            }
            break;
        }

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

    return numbers;
}

my solution

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!