以编程方式编辑联系人的姓名/电话号码 [英] Edit name/phone number of contact programmatically

查看:104
本文介绍了以编程方式编辑联系人的姓名/电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式修改联系人的显示名称:

I am trying to modify displayed name of a contact programmatically:

     try {
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
        .withSelection(ContactsContract.CommonDataKinds.Phone._ID + " = ?", new String[] {contact_id})
        .withValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "anything")
        .build());

        ContentProviderResult[] result = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        Log.w("UpdateContact", e.getMessage()+"");
        for(StackTraceElement ste : e.getStackTrace()) {
            Log.w("UpdateContact", "\t" + ste.toString());
        }
        Context ctx = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(ctx, "Update failed", duration);
        toast.show();
    }

contact_id是上次活动中收集的ContactsContract.CommonDataKinds.Phone._ID

contact_id is ContactsContract.CommonDataKinds.Phone._ID gathered in previous activity

代码执行正常,但是:

  • ContentProviderResult[]结果为空
  • 联系人姓名保持不变
  • ContentProviderResult[] result is null
  • Contact name remains unchanged

我也尝试过Data.DISPLAY_NAME,但效果相同.

I've experimented also with Data.DISPLAY_NAME but with same effect.

我阅读了以下手册: http://developer.android.com/guide/topics/provider/contacts-provider.html ,但我不想调用本机意图.

I read the Manual: http://developer.android.com/guide/topics/providers/contacts-provider.html but I don't want to call native intent.

谢谢.

推荐答案

您应在选择的内容中指定模仿类型.

You should specify the mimetype in your selection.

.withSelection(ContactsContract.CommonDataKinds.Phone._ID + "=? AND " +
               Data.MIMETYPE + "='" +
               ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'",
               new String[]{contact_id})

如果您还没有弄清楚,那就试试吧.我发现更新联系人在正确选择参数方面非常棘手.

If you haven't figured this out yet, give that a go. I found updating contacts to be very tricky in getting the selection arguments right.

这篇关于以编程方式编辑联系人的姓名/电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆