gpt4 book ai didi

android - 更新联系人显示名称

转载 作者:行者123 更新时间:2023-11-29 15:25:33 24 4
gpt4 key购买 nike

如何更新联系人的显示名称?下面代码中的操作在没有抛出任何东西的情况下完成并且似乎有效 - 也就是说,当我重新查询 ContactsContract.Contact 表时,返回了一行名称已更改。但是,当我尝试在我的平板电脑上运行股票“people”应用程序时,它崩溃了。显然我做错了什么。

这是代码。早期,它从聚合联系人中获取一个 id,如下所示,其中 key 是 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();

然后,在用户完成编辑后,我调用此代码块来更新显示名称:

  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;等我有更多内容要更新时再用。

老实说,我对自己实际使用的 ID 感到很困惑。这些名称对我来说不是那么清楚,我可能为此操作使用了错误的 ID。

就其值(value)而言,在更新后查看结果,我看到结果代码为 5。我找不到任何相关文档,所以不知道这是否重要。

最佳答案

ID(以及一般的更改联系人)可能会非常困惑......我也有一些戏剧性的想法。

这是我用于更新的一些工作代码。我能看到的主要区别是您如何声明原始 ID;它需要作为内容值包含在内。

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();
}

希望对您有所帮助!

关于android - 更新联系人显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com