Get a contact's groups?

前端 未结 3 1298
暖寄归人
暖寄归人 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

    A complete answer will be: First the fetch the group cursor (same as the answer above)

    Cursor groups_cursor= getContentResolver().query(
            ContactsContract.Groups.CONTENT_URI,
            new String[]{
                    ContactsContract.Groups._ID,
                    ContactsContract.Groups.TITLE
            }, null, null, null
    );
    

    Store all the group_id and group_title in a groups HashMap using this code:

     if(groups_cursor!=null){
            while(groups_cursor.moveToNext()){
                String group_title = groups_cursor.getString(1);
                String id = groups_cursor.getString(0);
                groups.put(id, group_title);
            }
        }
    

    Then using the above answer's data_cursor, fetch contacts_ids and their group_ids.

    Cursor dataCursor = getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            new String[]{
                    ContactsContract.Data.CONTACT_ID,
                    ContactsContract.Data.DATA1
            },
            ContactsContract.Data.MIMETYPE + "=?",
            new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}, null
    );
    

    Now using the dataCursor and the groups HashMap.

        if(dataCursor!=null){
                while(dataCursor.moveToNext()){
                    String id  = dataCursor.getString(0);
                    String group_id= dataCursor.getString(1);
                    String groupTitle = groups.get(group_id);
                    Log.d(TAG, "groupTitle : " + groupTitle + " contact_id: " + id );
               }
      }
    

提交回复
热议问题