更新联系人DISPLAY_NAME [英] update contacts display_name

查看:557
本文介绍了更新联系人DISPLAY_NAME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新联系人的显示名称?在下面的code的操作完成,没有抛出任何东西,似乎工作 - 也就是说,当我重新查询了ContactsContract.Contact表,一排带回来更名。然而,当我试图在我的平板电脑上运行的股票人的应用程序,它坠毁。 Evidentally我做错了什么。

How do I update the display name for a contact? The operation in the code below completes without throwing anything and appears to work - that is, when I requeried the ContactsContract.Contact table, a row came back with the name changed. However, when I tried running the stock "people" app on my tablet, it crashed. Evidentally I did something wrong.

下面是code。在早期,它将从总接触的ID如下所示,其中的的是lookup_key:

Here is the code. Early on, it fetches an id from the aggregate contacts as follows, where key is the lookup_key:

  String[] projection = new String[] {
    Contacts._ID, // 0
    Contacts.DISPLAY_NAME, // 1
  };

  Uri uri = Uri.parse (Contacts.CONTENT_LOOKUP_URI + "/" + key);
  ContentResolver cr = getContentResolver();
  Cursor cursor = cr.query (uri, projection, null, null, null);
  if (!cursor.moveToNext()) // move to first (and only) row.
    throw new IllegalStateException ("contact no longer exists for key");
  origId = cursor.getLong(0);
  cursor.close();

然后,用户做了他编辑后,我称之为code的此块更新DISPLAY_NAME:

Then, after the user has done his edits, I call this block of code to update the display_name:

  ArrayList<ContentProviderOperation> opers = new ArrayList<ContentProviderOperation>();
  ContentProviderOperation.Builder builder = null;

  String[] args = { Long.toString (origId) };
  builder = ContentProviderOperation.newUpdate (Data.CONTENT_URI);
  builder.withSelection (RawContacts.CONTACT_ID + "=?", args);
  builder.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, name);
  opers.add(builder.build());

  ContentProviderResult[] results = null;
  try {
    results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, opers);
  } catch ...

我知道我不需要为这个例子中ContentProviderOperation;这对后来当我有更多的东西来更新。

I realize I don't need the ContentProviderOperation for this example; that's for later when I have more stuff to update.

说实话,我是pretty混淆有关的ID,我实际使用。名称不说清楚我,我可能是使用了错误的ID进行此项操作。

To be honest, I'm pretty confused about which ID I'm actually using. The names aren't that clear to me and I may be using the wrong ID for this operation.

有关它的价值,看的结果在更新后我看到5.结果code我找不到任何文件,所以不知道,如果这是显著的。

For what it's worth, looking at results after the update I saw a result code of 5. I can't find any documentation for that, so have no idea if that is significant.

推荐答案

的ID(和一般的接触改变)可以pretty混乱...我有一些电视剧让我的头周围为好。

The IDs (and altering contacts in general) can be pretty confusing... I had some dramas getting my head around them as well.

下面是一些工作code我使用的更新。主要的区别我可以看到的是你如何申报的原始ID;它需要被包括在作为内容值

Here is some working code I use for updating. The main difference I can see is how you are declaring the raw ID; it needs to be included in as a content value.

Cursor cursor = _context.getContentResolver().query(contactUri,
            new String[] { Contacts._ID }, null, null, null);
    try {
        if (cursor.moveToFirst()) {
            String rawContactId = cursor.getString(0);
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            ContentValues contentValues = new ContentValues();
            contentValues.put(Data.RAW_CONTACT_ID, rawContactId);

            contentValues
                    .put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            contentValues.put(
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    phoneNumber);
            contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.TYPE_WORK);

            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                    .withValues(contentValues).build());
            String contactId = contactUri.getLastPathSegment();
            ops.add(ContentProviderOperation
                        .newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(
                                ContactsContract.Data.CONTACT_ID
                                        + "=? AND "
                                        + ContactsContract.Data.MIMETYPE
                                        + "=?",
                                new String[] {
                                        contactId,
                                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE })
                        .withValue(
                                ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                                newName).build());

            result = _context.getContentResolver().applyBatch(
                    ContactsContract.AUTHORITY, ops);
        }
    } finally {
        cursor.close();
    }

希望这有助于!

这篇关于更新联系人DISPLAY_NAME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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