Search code examples
androidcontacts

Modify native contact programmatically


I'm trying to modify contact first name and last name programmatically. The code snippet that I've used in order to do the job is the following one:

operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
  .withSelection( RawContacts._ID + "=?",
  new String[] { String.valueOf( mSmartphoneContactKey) } )
    .withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
     mContactName.getEditableText().toString() )
      .withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
        mContactLastName.getEditableText().toString() )                          
        .build() );

The mSmartphoneContactKey is filled in with the data contained in the column

ContactsContract.Contacts._ID

which is sitting in my projection array when I read contacts using content provider.

The problem is that for some contacts the name and last name are not modified and the phone type is modified instead. Actually I don't have any clue about the cause. Any advice is appreciated.


I've read further the documentation the Data table is the one I have to use. I've modified the code as below...still not working

        operations.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
                .withSelection( Data._ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
                         new String[] { String.valueOf( mSmartphoneContactId ) } )
                         .withValue( ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, mContactName.getEditableText().toString() )
                         .withValue( ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, mContactLastName.getEditableText().toString() )                            
                         .build() );

Please help me!


Solution

  • Ok Solved! Wrong ID passed. Need to retrieve the ID along the data from the DATA table.