2011-05-24 85 views
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查詢組?

回答

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; 
    } 

一個聯繫人可能位於多個組中,這裏它會回顧其第一組pnly。

我想這可能是給別人有用......

+0

它必須做這樣因爲GroupMembership沒有一個URI,僅MIME類型。爲什麼谷歌決定這是好的? – 2012-08-14 06:07:50