2011-12-14 13 views
1

定義済みのグループからすべての連絡先を削除しようとしていますが、連絡先テーブルとグループテーブルから参加を行う方法がわかりません。もちろんグループ内のすべての連絡先を削除する

ContentResolver cr = getContentResolver(); 
String where = ContactsContract.Groups.TITLE + " =='LolGroup'"; 

Cursor cursor = cr.query(
    ContactsContract.Contacts.CONTENT_URI, null, where, null, null); 
while (cursor.moveToNext()) { 
    String lookupKey = cursor.getString(
      cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    Uri uri = Uri.withAppendedPath(
     ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); 
    cr.delete(uri, null, null); 
} 

ない「タイトルは」連絡先グループには存在しないので、それは私にエラーを与えますが、私はIDと参加をすれば、私は私が望む結果を得る必要があります。

どのようにしてその参加を考えていますか?

答えて

0

ContactsContract.ContactsにContactsContract.Groups.TITLE列がないので、奇妙に見えます。ですから、あなたが望むグループタイトルを持つグループIDを取得してから、グループidを使って連絡先を検索することができます。この考えは次のようになるかもしれません:

public String getGroupIdByTitle(String groupTitle){ 
     try { 
      cursor = mContentResolver.query(
       ContactsContract.Groups.CONTENT_URI, 
        new String[] {Groups._ID}, 
        Groups.TITLE + "=?", 
        new String[]{groupTitle}, 
        null); 

        while (cursor.moveToNext()){ 
         return cursor.getString(cursor.getColumnIndex(0); 
        } 

     } finally { 
      if (cursor!=null) cursor.close(); 
     } 

     return ""; 
    } 

    public String getGroupIdOfContact(String lookupKey) { 

    String where = String.format("%s=? AND %s=?", Data.LOOKUP_KEY, Data.MIMETYPE); 
    String[] whereArgs = {lookupKey, GroupMembership.CONTENT_ITEM_TYPE}; 

    String groupRowId = ""; 
     Cursor cursor = mContentResolver.query(
       Data.CONTENT_URI, 
       new String[]{GroupMembership.GROUP_ROW_ID}, 
       where, whereArgs, null); 
     try { 
      if (cursor.moveToNext()) { 
       return cursor.getString(cursor.getColumnIndex(GroupMembership.GROUP_ROW_ID)); 
      } 

     } finally { 
      if (cursor!=null) cursor.close(); 
     } 

return ""; 
    } 

    public void deleteContactByGroupTitle(String groupTitle) { 

     String targetGroupId = getGroupIdByTitle(groupTitle); 

     Cursor cursor = null; 
     try { 
      cursor = mContentResolver.query(Contacts.CONTENT_URI, null, null, null, null); 

      while (cursor.moveToNext()){ 
      String lookupKey = cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY)); 
       String groupId = getGroupIdOfContact(lookupKey); 
       if (targetGroupId.equals(groupId)){ 
         //TODO. delete this contact 
       } 
      } 

     } finally { 
      if (cursor!=null) cursor.close(); 
     } 

    } 

上記のコードはテストされていませんが、基本的な考え方は同じであると思います。

関連する問題