0

I am making an android app, I want to remove a contact from a specific group not to delete contact just remove from the group, I have group id and contact id, can anyone please tell me the query to do this, I want to implement something like Delete contact_id=1 from group_id=2

4

2 回答 2

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);
于 2012-09-20T07:11:15.927 回答
1
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;
}
于 2017-03-16T16:36:55.447 回答