我希望我的代码能够在Android联系簿中更新联系人详细信息(如姓名,电话号码,电子邮件,组织详细信息等)。我成功地修改了一些(名称,电话号码和电子邮件是具体的)但不是全部。
每当我尝试更新联系人的组织详细信息(Contacts.Organizations.COMPANY和Contacts.Organizations.TITLE)时,我的应用都会抛出异常
java.lang.UnsupportedOperationException: Cannot update URL: content://contacts/people/69/organizations/69
代码段如下:
Uri baseUri = ContentUris.withAppendedId(People.CONTENT_URI, 69);
Uri uri = Uri.withAppendedPath(baseUri, People.Phones.CONTENT_DIRECTORY);
Cursor c = this.getContentResolver().query(uri,
new String[] { Contacts.Organizations._ID, Contacts.Organizations.COMPANY,Contacts.Organizations.TITLE},
null, null, null);
if(c.getCount() > 0) {
uri = ContentUris.withAppendedId(uri, c.getString(0));
ContentValues val1 = new ContentValues();
val1.put(Contacts.Organizations.COMPANY, "arw");
val1.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
val1.put(Contacts.Organizations.TITLE, "abcdef");
this.getContentResolver().insert(uri, val1);
答案 0 :(得分:2)
要更新联系人中的任何数据,您需要知道该字段的现有ID。然后我使用Builder类为我想要更新的各个字段获取单独的ContentProviderOperation对象,将它们添加到arrayList然后使用ContentProvider.applyBatch()方法
答案 1 :(得分:1)
我花了很多时间尝试更新公司和标题信息。以下现在对我有用。
Uri workUri = Uri.withAppendedPath(contactUri, android.provider.Contacts.Organizations.CONTENT_DIRECTORY);
values.clear();
values.put(android.provider.Contacts.Organizations.COMPANY, "company");
values.put(android.provider.Contacts.Organizations.TYPE, android.provider.Contacts.Organizations.TYPE_WORK);
values.put(android.provider.Contacts.Organizations.TITLE, "job title");
values.put(android.provider.Contacts.Organizations.ISPRIMARY, 1);
getContentResolver().insert(workUri, values);
答案 2 :(得分:1)
String[] projection = new String[] { ContactsContract.RawContacts._ID };
String where = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selection = new String[] { String.valueOf(1) };
Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, where, selection, null);
c.moveToFirst();
for(int i=0;i<c.getCount();i++)
{
Log.e("id",""+c.getString(0));
c.moveToNext();
}
ContentValues values = new ContentValues();
values.clear();
values.put(ContactsContract.RawContacts._ID , 0);
getContentResolver().update(ContactsContract.RawContacts.CONTENT_URI, values,ContactsContract.RawContacts._ID + "+ ?",new String[] { String.valueOf(1) });