Deqing
Deqing

Reputation: 14632

How to get group id by title?

I'd like to add a contact member to a group:

values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId);

Currently I only have TITLE of this group, how do I get groupId by this TITLE?

Upvotes: 1

Views: 1329

Answers (2)

Deqing
Deqing

Reputation: 14632

Ok, this is a full version of how to get the group id by its title. Basically it iterates all groups and compare titles to find its id.

private String getGroupId(String groupTitle) {
    Cursor cursor = getContentResolver().query(ContactsContract.Groups.CONTENT_URI,new String[]{ContactsContract.Groups._ID,ContactsContract.Groups.TITLE}, null, null, null);     
    cursor.moveToFirst();
    int len = cursor.getCount();

    String groupId = null;
    for (int i = 0; i < len; i++) {
        String id = cursor.getString(cursor.getColumnIndex(Groups._ID));
        String title = cursor.getString(cursor.getColumnIndex(Groups.TITLE));

        if (title.equals(groupTitle)) {
            groupId = id;
            break;
        }
        cursor.moveToNext();
    }
    cursor.close();

    return groupId;
}

Upvotes: 1

Jans
Jans

Reputation: 11250

If what you want is to access groupId by group name you can call getContentResolver().query( ... ) on content provider by passing ContactsContract.Contacts.DISPLAY_NAME column name and it respectivelyvalue.

Also you can look this seemed post Getting Contact GroupName and GroupId

Upvotes: 0

Related Questions