【问题标题】:How to update Android contact company?如何更新安卓联系人公司?
【发布时间】:2011-07-31 20:21:20
【问题描述】:

我最近为我的应用创建了一个同步适配器,它会将我通过网络请求获得的联系人与手机中的联系人同步。我添加联系人没有问题,但是当联系人信息发生更改时,我无法正确更新联系人信息。例如,联系人上的公司名称字段。以下是我尝试过的一些示例查询,但它们不起作用或仅部分起作用(即 - 一些联系人已更新但不正确):

        ContentValues values = new ContentValues();
    values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company");
context.getContentResolver().update(Uri.parse("content://com.android.contacts/data/"), values, BaseColumns._ID + "=?", new String[] { String.valueOf(id) } );

我也尝试按照 android 文档的建议批量执行此操作:

    builder = ContentProviderOperation
            .newUpdate(ContactsContract.Data.CONTENT_URI);
    builder.withSelection(BaseColumns._ID + " =?", new String[]{String.valueOf(id)});

    builder.withValue(
            ContactsContract.CommonDataKinds.Organization.COMPANY,
            "New Company Name!");
    operationList.add(builder.build());

我已阅读ContactContracts Documentation 并最初关注此tutorial。我还检查了 api 中的AuthenticatorActivity 示例,但无济于事。非常感谢任何帮助。

【问题讨论】:

    标签: android contacts sync contactscontract android-contacts


    【解决方案1】:

    在花费大量时间试图找出正确的查询之后,我相信我已经找到了答案。看起来我需要将 BaseColumns._ID 更改为 ContactsContract.Data.CONTACT_ID 并且对于我所做的每次更新,我还必须提供 mime 类型,而且我在 android 文档中的任何地方都没有看到这个。在这篇文章中找到了很多帮助:Working With Android Contacts

            String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
        String[] orgWhereParams = new String[]{String.valueOf(id), 
            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
        operationList
        .add(ContentProviderOperation
                .newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(orgWhere, orgWhereParams)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.DATA,
                        guCon.getCompany()).build());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多