5

我想检索联系人详细信息及其所属的组。我得到了列出手机中所有联系人组的代码。

Cursor groupC = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI, null, null, null, null); 

while (groupC.moveToNext()) { 
    String groupid =
        groupC.getString(groupC.getColumnIndex(ContactsContract.Groups._ID));
    Log.e("myTag", groupid); 
    String grouptitle =
        groupC .getString(groupC.getColumnIndex(ContactsContract.Groups.TITLE));
    Log.e("myTag", grouptitle);
}
groupC.close();

然后我尝试使用其 id 查询特定联系人,但它总是显示There is no such column...

Cursor groupC = getContentResolver().query(
    ContactsContract.Groups.CONTENT_URI,
    null,
    ContactsContract.Contacts._ID+"= ?",
    new String[]{id},
    null);

id 在哪里

Cursor cur = cr.query(
    ContactsContract.Contacts.CONTENT_URI,
    null,
    null,
    null,
    null);
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

如何使用特定的联系人 ID 查询组?

4

1 回答 1

2

我找到了答案。我们应该传递原始联系人 ID 和正确的 mime 类型。

  String where = ContactsContract.Data.RAW_CONTACT_ID
            + "="
            + Integer.parseInt(id)
            + " AND "
            + ContactsContract.Data.MIMETYPE
            + "='"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            + "'";

    Cursor cursor = ctx
            .getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI, null, where, null,
                    null);
    startManagingCursor(cursor);
    Log.e("Count is:", ""+ cursor.getCount());
    while (cursor.moveToNext()) {
        groupid = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
        Log.e("groupid", groupid);
        builder.append(groupid);

    }String where = ContactsContract.Data.RAW_CONTACT_ID
            + "="
            + Integer.parseInt(id)
            + " AND "
            + ContactsContract.Data.MIMETYPE
            + "='"
            + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
            + "'";

    Cursor cursor = ctx
            .getContentResolver()
            .query(ContactsContract.Data.CONTENT_URI, null, where, null,
                    null);
    startManagingCursor(cursor);
    Log.e("Count is:", ""+ cursor.getCount());
    while (cursor.moveToNext()) {
        groupid = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
        Log.e("groupid", groupid);
        break;
    }

一个联系人可能在多个组中,这里它只检索其第一组。

我认为这可能对某人有用...

于 2011-06-02T04:44:16.420 回答