检索特定联系人组

时间:2011-05-24 09:39:25

标签: android android-contacts

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

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查询群组?

1 个答案:

答案 0 :(得分:2)

我找到了答案。我们应该传递原始的contact-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;
    }

联系人可能在多个群组中,在这里它会追溯其第一个群体。

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