Modify native contact programmatically

风流意气都作罢 提交于 2019-12-08 13:37:26

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/10603187/modify-native-contact-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!