Get a contact's groups?

前端 未结 3 1302
暖寄归人
暖寄归人 2020-12-16 01:05

I\'m trying to make a many-to-many mapping of contacts to groups.

For example, if I have:

  • User 1, belongs to group 701, 702, 704
  • User 2, belon
3条回答
  •  余生分开走
    2020-12-16 01:17

     public static HashMap 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 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;
    }
    

提交回复
热议问题