【问题标题】:Why am I unable to update the email address in android?为什么我无法在 android 中更新电子邮件地址?
【发布时间】:2012-12-03 01:09:01
【问题描述】:
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.clear();

    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(Data._ID + "=?", new String[]{String.valueOf(id)})
            .withValue(Email.DATA, "somebody1@android.com")
            .build());

    try 
    {
        context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }

日志没有显示任何内容。但是邮件没有更新。有谁知道为什么?

转成字符串的ops如下:

[mType: 2, mUri: content://com.android.contacts/data, mSelection: _id=?, mExpectedCount: null, mYieldAllowed: false, mValues: data1=somebody1@android.com, mValuesBackReferences: null, mSelectionArgsBackReferences: null]

【问题讨论】:

  • 您是否尝试过使用 Log.d('tag',VALUEasSTRING);检查你的价值观?
  • 你指的是哪个值?身份证?如果是的话
  • 实际上所有的值,但听起来你已经尝试过了
  • 我也遇到了这个问题。不管我做什么,它似乎都没有更新。我的ContentProviderOperation 看起来一模一样。
  • id 来自哪里?我的猜测是它可能是其他东西,例如原始联系人 ID,这意味着选择将为空,因此不会执行更新。 @乔纳

标签: android android-contacts contactscontract android-contentresolver


【解决方案1】:

首先,您有一个try 没有catch,这可以解释为什么您在日志中看不到任何错误。

其次,从我在文档中看到的,我认为你最好这样写:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.clear();

ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
        .withSelection(ContactsContract.Data.CONTACT_ID + "=?", 
                       new String[] {String.valueOf(id)})
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, "somebody1@android.com")
        .build());

try {
    context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
    e.printStackTrace();
}

【讨论】:

    【解决方案2】:

    尝试在 withValue 中使用不是字符串,而是字符串数组。

    .withValue(ContactsContract.CommonDataKinds.Email.DATA, emails)
    

    在哪里

    String[] emails = {"Email Name<somebody1@android.com>"};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      相关资源
      最近更新 更多