How to update your Android contact company?

I recently created a sync adapter for my application. It synchronizes the contacts that I receive through a web request with the contacts in the phone. I have no problem adding a contact, but I can’t get the contact information to properly update the information when the contact information has changed. For example, the "Company name" field on the contact. Here are some examples of requests that I tried that didn’t work or partially worked (i.e. some contacts are updated, but not correctly):

ContentValues values = new ContentValues(); values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company"); context.getContentResolver().update(Uri.parse("content://com.android.contacts/data/"), values, BaseColumns._ID + "=?", new String[] { String.valueOf(id) } ); 

I also tried doing this in a package, as suggested in the Android documentation:

  builder = ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI); builder.withSelection(BaseColumns._ID + " =?", new String[]{String.valueOf(id)}); builder.withValue( ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company Name!"); operationList.add(builder.build()); 

I read the ContactContracts Documentation and originally followed this tutorial . I also checked the AuthenticatorActivity example in the api to no avail. Any help is appreciated.

+4
source share
1 answer

After spending exhaustive time trying to figure out the right query, I think I found the answer. It seems like I need to change BaseColumns._ID in ContactsContract.Data.CONTACT_ID and for each update I did, I also had to put a mime type and I did not see it anywhere in the android documentation. Great help was found in this review: Working with Android Contacts

  String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] orgWhereParams = new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; operationList .add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(orgWhere, orgWhereParams) .withValue( ContactsContract.CommonDataKinds.Organization.DATA, guCon.getCompany()).build()); 
+9
source

Source: https://habr.com/ru/post/1347127/


All Articles