Android-如何通过组选择删除联系人

Android-如何通过组选择删除联系人,android,android-contacts,Android,Android Contacts,我正在尝试删除确定组中的所有联系人 我已经创建了group\u ROW\u ID=987的组 因此,我应该删除组行ID=987的联系人 我不知道如何删除它们。 有人能帮我吗?删除记录 要删除单个记录,请使用特定行的URI调用{ContentResolver.delete() 要删除多行,请调用ContentResolver.delete(),使用要删除的记录类型的URI(例如,android.provider.Contacts.People.CONTENT\u URI)和定义要删除哪些行的SQL

我正在尝试删除确定组中的所有联系人

我已经创建了
group\u ROW\u ID=987的组

因此,我应该删除组行ID=987的联系人

我不知道如何删除它们。 有人能帮我吗?

删除记录

要删除单个记录,请使用特定行的URI调用{
ContentResolver.delete()

要删除多行,请调用
ContentResolver.delete()
,使用要删除的记录类型的URI(例如,
android.provider.Contacts.People.CONTENT\u URI
)和定义要删除哪些行的SQL WHERE子句。(注意:如果要删除常规类型,请确保包含有效的WHERE子句,否则可能会删除比预期更多的记录!)



遍历每个联系人并删除每个记录:

我在一个查询中连接两个表的另一个示例

private static final String JOIN_QUERY=
 "SELECT * FROM Employee JOIN User ON userId = employeeUserId";

如果你还没有解决这个问题,我就是这么做的。我绝对不认为这是一个有效的解决方案。这只是一个简单的解决方案

首先查找具有特定组id的所有联系人id。然后为每个要删除的联系人创建
ContentProviderOperation
,最后应用删除操作列表

private void deletaAllInGroup(Context context, long groupId)
   throws RemoteException, OperationApplicationException{
    String where = String.format("%s = ?", GroupMembership.GROUP_ROW_ID);
    String[] whereParmas = new String[] {Long.toString(groupId)};
    String[] colSelection = new String[] {Data.CONTACT_ID};

    Cursor cursor = context.getContentResolver().query(
            Data.CONTENT_URI, 
            colSelection, 
            where, 
            whereParmas, 
            null);

    ArrayList<ContentProviderOperation> operations = 
        new ArrayList<ContentProviderOperation>();

    // iterate over all contacts having groupId 
    // and add them to the list to be deleted
    while(cursor.moveToNext()){ 
        String where = String.format("%s = ?", RawContacts.CONTACT_ID);
        String[] whereParams = new String[]{Long.toString(cursor.getLong(0))};

        operations.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
        .withSelection(where, whereParams)
        .build());
    }

    context.getContentResolver().applyBatch(
        ContactsContract.AUTHORITY, operations );
}
private void deletaAllInGroup(上下文,长groupId)
引发RemoteException、OperationApplicationException{
字符串,其中=String.format(“%s=?”,GroupMembership.GROUP\u ROW\u ID);
字符串[],其中parmas=新字符串[]{Long.toString(groupId)};
String[]colSelection=新字符串[]{Data.CONTACT_ID};
Cursor Cursor=context.getContentResolver().query(
Data.CONTENT_URI,
colSelection,
哪里
帕尔马斯,
无效);
ArrayList操作=
新的ArrayList();
//迭代具有groupId的所有联系人
//并将它们添加到要删除的列表中
while(cursor.moveToNext()){
字符串,其中=String.format(“%s=?”,RawContacts.CONTACT\u ID);
字符串[],其中参数=新字符串[]{Long.toString(cursor.getLong(0))};
添加(ContentProviderOperation.newDelete(RawContacts.CONTENT\u URI)
.选举(何处、何处)
.build());
}
context.getContentResolver().applyBatch(
联系人(合同、授权、运营);
}

好的,但我的问题是找到正确的联系人,我找不到一个字段来告诉我联系人在一个确定的组中。我看到的是,我必须查看组表并获取其中的人的id,但我失败了:D那么…我如何匹配2个表?有没有办法加入他们?那么…哪个字段匹配他们?我看到了联系人CONTrat有很多子表,我想所有的表都有一个匹配联系人的键,但我不知道是哪一个…第一个表是SELECT*FROM view\u groups,第二个表是SELECT*FROM view\u raw\u contacts\u restricted,其中(1)…我从哪里获得联系人?>。
private final String MY_QUERY = "SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.other_id WHERE b.property_id=?";

db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
private static final String JOIN_QUERY=
 "SELECT * FROM Employee JOIN User ON userId = employeeUserId";
private void deletaAllInGroup(Context context, long groupId)
   throws RemoteException, OperationApplicationException{
    String where = String.format("%s = ?", GroupMembership.GROUP_ROW_ID);
    String[] whereParmas = new String[] {Long.toString(groupId)};
    String[] colSelection = new String[] {Data.CONTACT_ID};

    Cursor cursor = context.getContentResolver().query(
            Data.CONTENT_URI, 
            colSelection, 
            where, 
            whereParmas, 
            null);

    ArrayList<ContentProviderOperation> operations = 
        new ArrayList<ContentProviderOperation>();

    // iterate over all contacts having groupId 
    // and add them to the list to be deleted
    while(cursor.moveToNext()){ 
        String where = String.format("%s = ?", RawContacts.CONTACT_ID);
        String[] whereParams = new String[]{Long.toString(cursor.getLong(0))};

        operations.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
        .withSelection(where, whereParams)
        .build());
    }

    context.getContentResolver().applyBatch(
        ContactsContract.AUTHORITY, operations );
}