【问题标题】:How to delete all android contacts that start with a specific name?如何删除以特定名称开头的所有 android 联系人?
【发布时间】:2015-07-22 16:10:00
【问题描述】:

我想从我的安卓手机中删除所有以“AAA”开头或包含“AAA”的联系人。这是我尝试过的:

private void deleteContact(String name) {

        ContentResolver cr = getContentResolver();
        String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
        String[] params = new String[] {"AAAA"};

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
                .withSelection(where, params)
                .build());
        Log.e(",,,,",String.valueOf(ops.get(0)));
        try {


            cr.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Toast.makeText(NativeContentProvider.this, "Deleted the contact with name '" + name +"'", Toast.LENGTH_SHORT).show();

    }

但是失败了。请给我一些想法,以便我可以继续我的项目。

【问题讨论】:

  • 失败了怎么办?请详细说明您遇到了什么错误或问题。

标签: android android-contentprovider android-contacts


【解决方案1】:

也许这会让你走上正轨:

ContentResolver mContentResolver = getContentResolver();
    private int deleteContactsLike(String name) {
    return mContentResolver.delete(
            ContactsContract.RawContacts.CONTENT_URI,
            ContactsContract.Contacts.DISPLAY_NAME
            + " like ?",
            new String[] { name + '%'});

【讨论】:

  • 这是我得到的错误:android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compile: SELECT _id, contact_id FROM view_raw_contacts WHERE display_name like = ?
  • 第一件事:您的查询中不应有“=”。在我上面的示例中,它使用“like”运算符。第二件事:如果您尝试删除记录,则不应有“选择”语句。您应该使用内容解析器的删除方法。
  • 先生,贝克先生,您这样做是正确的,但我正处于 android 的启动阶段,先生,您可以远程控制我的系统吗,因为我刚刚卡在我的项目中间问题
猜你喜欢
  • 1970-01-01
  • 2020-04-07
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
相关资源
最近更新 更多