繁体   English   中英

如何按标题获取组ID?

[英]How to get group id by title?

我想将联系人添加到组中:

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

目前,我只有这个群组的TITLE ,如何通过这个TITLE获取groupId

好的,这是如何通过标题获取组ID的完整版本。 基本上,它会迭代所有组并比较标题以找到其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;
}

如果要通过group name访问groupId ,则可以通过传递ContactsContract.Contacts.DISPLAY_NAME列名及其相应值来在内容提供者上调用getContentResolver().query( ... )

你也可以看一下这个看起来像帖子Getting Contact GroupName and GroupId

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM