以下代码用于更新联系人。我能够更新电话号码字段,但我正在尝试更新联系人姓名(在评论行内),但我无法理解代码,谁能帮我解决这个问题。
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();