如何获取联系人组ID或名称?

时间:2011-05-19 10:19:24

标签: java android

我无法获取存储联系人的组名称。我可以得到它是否在任何组中添加为布尔值(IN_VISIBLE_GROUP)。我不知道如何获取组名称或ID。

     ContentResolver cr = this.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
     if (cur.getCount() > 0) {
     while (cur.moveToNext()) 
            {

                 id = cur.getString(cur
                        .getColumnIndex(BaseColumns._ID));

                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                String group = cur
                .getString(cur
                        .getColumnIndex(ContactsContract.Contacts.IN_VISIBLE_GROUP));

我尝试过使用ContactsContract.Groups和ContactsContract.Groups以及ContactsContract.CommonDataKinds.GroupMembership,但这不是解决方案。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

请更改

 ContentResolver cr = this.getContentResolver();

 Uri groupURI = ContactsContract.Data.CONTENT_URI;


Cursor cur = cr.query(groupURI , null, null, null, null);


// Now you can get group ID from cursor

String groupRowId =  cur.getString   (cur.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));