可以从收到的电话号码中获取ID,但不能从组中获取

时间:2012-01-03 18:42:21

标签: java android contacts contactscontract

我已成功从正在呼叫的电话号码中获取ID和姓名。我想要查看此ID所属的组。我尝试过以下方法:

    //Search for the information about the phone number, save the goupID(s)
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(aNumber)); 
    ContentResolver cr = mService.getContentResolver();
    Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);

    myCursor.moveToFirst();
    //String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID)); 
    String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    myCursor.close();

    //Use the cursor to query for group with help of ID from the Phone look up
    myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
            new String[]{ContactsContract.Groups._ID},
            ContactsContract.Groups._ID + " = ?",
            new String[]{contactID}, 
            null);      

    //Contact may be in more than one group
    nbrOfGroups = myCursor.getCount(); 
    groupName = new String [nbrOfGroups];

问题在于第二个查询,我想使用在电话查找中找到的contactID来查看contacID所属的组。结果是没有组,尽管联系人已添加到我的联系人组中。

有什么想法吗? :)

1 个答案:

答案 0 :(得分:1)

Groups._ID与Contact ID不同,而是存储所有组信息的表的索引。获得联系人ID后,您应该使用组成员身份mimetype从数据表中获取该联系人的所有组成员身份。

获取组ID后,您可以查询“组”表以获取所有组的标题

尝试使用此代码

    //Search for the information about the phone number, save the goupID(s)
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("123123")); 
    ContentResolver cr = this.getContentResolver();
    Cursor myCursor = cr.query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},null, null, null);

    myCursor.moveToFirst();
    //String contactID = myCursor.getString(myCursor.getColumnIndex(PhoneLookup._ID)); 
    String contactID = myCursor.getString(myCursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    myCursor.close();

    //Use the cursor to query for group with help of contact ID from the Phone look up
    myCursor = cr.query(ContactsContract.Data.CONTENT_URI,
            new String[]{ContactsContract.Data.CONTACT_ID, 
            ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID},
            ContactsContract.Data.CONTACT_ID + " = ? " + 
            Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'",
            new String[]{contactID}, 
            null);      

    //Contact may be in more than one group
    int nbrOfGroups = myCursor.getCount();
    int[] groupIds = new int[nbrOfGroups];
    int index = 0;

    // unfortunately group names are stored in Groups table
    // so we need to query again
    if (myCursor.moveToFirst()) {
        do {
            groupIds[index] = myCursor.getInt(1); // Group_row_id column
        } while (myCursor.moveToNext());
    }

    myCursor.close();

    // construct the selection
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nbrOfGroups; i++) {
        if (i != 0) {
            sb.append(",");
        }

        sb.append(groupIds[i]);
    }

    String[] groupName = new String [nbrOfGroups];
    myCursor = cr.query(ContactsContract.Groups.CONTENT_URI,
            new String[]{ContactsContract.Groups.TITLE},
            ContactsContract.Groups._ID + " IN (" + sb.toString() + ")",
            null, null);

    // finally got the names
    if (myCursor.moveToFirst()) {
        do {
            groupName[index] = myCursor.getString(0); // Group_row_id column
        } while (myCursor.moveToNext());
    }

    myCursor.close();