如何在 Android 中编辑现有联系人?
Posted
技术标签:
【中文标题】如何在 Android 中编辑现有联系人?【英文标题】:How to edit existing Contacts in Android? 【发布时间】:2016-02-01 22:27:06 【问题描述】:我正在尝试使用应用程序编辑现有的电话联系人。通过使用联系人姓名获取相应的联系人 ID。然后尝试使用联系人 ID 更新现有联系人。但不幸的是它没有更新。
我的代码如下,
String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" + edt_nameDetail.getText() + "\" )";
Cursor c = getActivity().getApplicationContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
getActivity().startManagingCursor(c);
if (c.moveToNext())
ContactId = c.getString(0);
Log.e("Tag contact id ","edit contact id "+ ContactId);
try
String name = edt_nameDetail.getText().toString().trim();
String email = edt_contactEmailDetail.getText().toString().trim();
String number = edt_mobileNumberDetail.getText().toString().trim();
ContentResolver contentResolver = getActivity().getContentResolver();
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] emailParams = new String[]ContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE;
String[] nameParams = new String[]ContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE;
String[] numberParams = new String[]ContactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();
if(!email.equals("") &&!name.equals("")&& !number.equals(""))
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,emailParams)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.build());
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,nameParams)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,numberParams)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.build());
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(getActivity(), "Contact is successfully edited", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Fail edit", Toast.LENGTH_SHORT).show();
【问题讨论】:
***.com/questions/8490123/… 【参考方案1】:如果您使用电子邮件进行搜索,请将您的 .withSelection 更新为
.withSelection(ContactsContract.CommonDataKinds.Email.ADDRESS + "=? AND " +
Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'",
new String[]email)
根据您的搜索类型更新所有选择代码
【讨论】:
正在使用联系人 ID 进行搜索【参考方案2】:注意:在此之后,联系人 ID 将更改。
public static boolean updateContactName(String contactId, String pre, String first, String mid, String last, String suf)
try
if (pre == null && first == null && mid == null && last == null && suf == null)
return false;
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] nameParams = new String[]contactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE;
ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<>();
android.content.ContentProviderOperation.Builder t ;
android.content.ContentProviderOperation b ;
t = android.content.ContentProviderOperation.newUpdate(Data.CONTENT_URI);
t = t.withSelection(where, nameParams);
if(pre != null)
t = t.withValue(StructuredName.PREFIX, pre.trim());
if(first != null)
t = t.withValue(StructuredName.GIVEN_NAME, first.trim());
if(mid != null)
t = t.withValue(StructuredName.MIDDLE_NAME, mid.trim());
if(last != null)
t = t.withValue(StructuredName.FAMILY_NAME, last.trim());
if(suf != null)
t = t.withValue(StructuredName.SUFFIX, suf.trim());
b = t.build();
ops.add(b);
ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return true;
catch (Exception e)
return false;
【讨论】:
以上是关于如何在 Android 中编辑现有联系人?的主要内容,如果未能解决你的问题,请参考以下文章
在模态中编辑现有联系人(bootstrap-ui angular)
如何在 Android Studios 中更新联系人的电话号码