gpt4 book ai didi

android - 无法更新联系方式

转载 作者:行者123 更新时间:2023-11-29 00:27:51 25 4
gpt4 key购买 nike

以下代码是更新联系人。我可以更新电话号码字段,但我正在尝试更新联系人姓名(在评论行内),但我不可能理解代码,任何人都可以帮助我解决这个问题。

String new_phoneNumber = update_phonenumber.getText()
.toString();
String new_name = update_name.getText().toString();
ContentResolver cr = getContentResolver();

String where = ContactsContract.Data.DISPLAY_NAME
+ " = ? AND "
+ ContactsContract.Data.MIMETYPE
+ " = ? AND "
+ String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE)
+ " = ? ";
String[] params = new String[] {
get_name,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) };

Cursor phoneCur = managedQuery(
ContactsContract.Data.CONTENT_URI, null, where, params,
null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

if ((null == phoneCur)) {
Toast.makeText(getApplicationContext(), "Empty Contact",
Toast.LENGTH_LONG).show();
} else {
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(
ContactsContract.CommonDataKinds.Phone.DATA,
new_phoneNumber)/***.withValue(
ContactsContract.CommonDataKinds.Phone.DATA,
new_name)***/
.build());
}

phoneCur.close();

try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Toast.makeText(getApplicationContext(), "Successfully updated",
Toast.LENGTH_LONG).show();

最佳答案

您可以在您的代码中添加另一个 ops.add 并插入以下代码。

ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.DATA1 + "=?", new String[] {get_name})
.withValue(
StructuredName.DISPLAY_NAME,
new_name)
.build());

关于android - 无法更新联系方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18071408/

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