Android使用名字和姓氏删除联系人

时间:2014-10-24 20:36:02

标签: android

我正在尝试使用名字和姓氏删除联系人。我也确定这个问题在我的选择范围内,但我不确定应该改变什么。

我收到错误。致命异常:主要android.database.SQLiteException:没有这样的列:data2(代码1):,同时编译:SELECT _id,contact_id FROM view_raw_contacts WHERE data2 =? AND data3 =?

我想知道我的问题在哪里?

    private void deleteContact(String firstName, String lastName) {

        String selection = "";

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ContentProviderOperation.Builder op = ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI);

        String[] selectionArgs = new String[] {firstName, lastName};
        selection = ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME + " = ? ";
        op.withSelection(selection, selectionArgs);
        ops.add(op.build());

        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {

            e.printStackTrace();
        } catch (OperationApplicationException e) {

            e.printStackTrace();
        }
    }

下面的代码使用显示名称删除联系人。这就是为什么它让我相信问题在于我如何使用它:ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME

我在尝试此操作时收到了同样的错误:selection = String.valueOf(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)+&#34; =? AND&#34; + String.valueOf(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)+&#34; =? &#34 ;;

private void deleteContact(String displayName) {

    String selection = "";

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation.Builder op = ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI);

        String[] selectionArgs1 = new String[] {displayName};
        selection = ContactsContract.Data.DISPLAY_NAME + " = ? ";
        op.withSelection(selection, selectionArgs1);
        ops.add(op.build());

    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {

        e.printStackTrace();
    } catch (OperationApplicationException e) {

        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:1)

这对我有用。不确定是否有不同的方法来做到这一点。

private void deleteContact(String firstName, String lastName) {

    String selection = "";

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation.Builder op = ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI);

    String[] selectionArgs = new String[] {firstName + " " + lastName};
    selection = ContactsContract.Data.DISPLAY_NAME + " = ? ";
    op.withSelection(selection, selectionArgs);
    ops.add(op.build());

    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {

        e.printStackTrace();
    } catch (OperationApplicationException e) {

        e.printStackTrace();
    }
}

答案 1 :(得分:0)

更新:

ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME是一个字符串,因此必须用引号括起来试试这个:

  

“'”+ ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME +“'”+ .....