2012-08-27 3 views
0

나는 안드로이드 앱을 만들고 있습니다. 그룹에서 연락처를 삭제하지 않으려면 그룹에서 삭제하고 싶습니다. 그룹 ID와 연락처 ID가 있습니다. 누구든지 알려주십시오. 나를 쿼리 할 같은 것을 구현하고 싶습니다. contact_id = 1 from group_id = 2에서android에서 특정 그룹의 연락처 삭제하기

답변

1

연락처는 ContactsContract.CommonDataKinds.GroupMembership 레코드가있는 그룹에 연결됩니다. 당신은 그룹에서 연락처를 삭제하려면이 같은 것을 사용할 수 있습니다

private void deleteContactFromGroup(long contactId, long groupId) 
{ 
    ContentResolver cr = getContentResolver(); 
    String where = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + groupId + " AND " 
      + ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID + "=?" + " AND " 
      + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='" 
      + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'"; 

    for (Long id : getRawContactIdsForContact(contactId)) 
    { 
     try 
     { 
      cr.delete(ContactsContract.Data.CONTENT_URI, where, 
        new String[] { String.valueOf(id) }); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

private HashSet<Long> getRawContactIdsForContact(long contactId) 
{ 
    HashSet<Long> ids = new HashSet<Long>(); 

    Cursor cursor = getContentResolver().query(RawContacts.CONTENT_URI, 
       new String[]{RawContacts._ID}, 
       RawContacts.CONTACT_ID + "=?", 
       new String[]{String.valueOf(contactId)}, null); 

    if (cursor != null && cursor.moveToFirst()) 
    { 
     do 
     { 
      ids.add(cursor.getLong(0)); 
     } while (cursor.moveToNext()); 
     cursor.close(); 
    } 

    return ids; 
} 

삭제 수행 할 때, 당신은 RAW_CONTACT_ID 대신 CONTACT_ID를 지정해야합니다. 따라서 특정 연락처에 대해 모든 원시 컨택 ID를 쿼리해야합니다.

계정 데이터를 고려해야 할 수도 있습니다. 그런 일에 접촉 ID에 대한 쿼리 경우의 변경 :

Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon() 
      .appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName) 
      .appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType).build(); 

Cursor cursor = getContentResolver().query(rawContactUri, 
      new String[] { RawContacts._ID }, RawContacts.CONTACT_ID + "=?", 
      new String[] { String.valueOf(contactId) }, null); 
0
public static Uri addContactToGroup(String rawContactId,String groupId) 
    { 
     try 
     { 

      ContentValues values = new ContentValues(); 
      values.put(Data.RAW_CONTACT_ID, rawContactId); 
      values.put(GroupMembership.GROUP_ROW_ID, groupId); 
      values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); 

      return getContentResolver.insert(Data.CONTENT_URI, values); 
     } 
     catch (Exception e) 
     {} 
     return Uri.EMPTY; 
    } 

// ------------------------- ----------

public static int removeContactFromGroup(String contactId,String groupId) 
{ 
    try 
    { 
     String where = Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ? AND " + GroupMembership.GROUP_ROW_ID + " = ?"; 
     String[] args = {contactId, GroupMembership.CONTENT_ITEM_TYPE, groupId}; 
     return getContentResolver.delete(Data.CONTENT_URI, where, args); 
    } 
    catch (Exception e) 
    {} 
    return 0; 
} 
관련 문제