简体   繁体   中英

I want to add information into the contact according to the ID number, but it always being added to the wrong name.

I want to add a telephone number into the contact according to the ID number, but it always being added to the wrong name. Can anyone tell me the reason why this happened. It runs well in virtual device and sony mobile phone , but it come to an error as I said above in a new Samsung moble phone. I can clearly confirm that the ID number is right These is the source code:

ContentValues values = new ContentValues (); 
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactID);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, shortNumber);
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_OTHER);
getContentResolver().insert(Data.CONTENT_URI,values); 
values.clear();

The answer here may help: Inserting contacts in Android 2.2 .

It seems the code above does not work on all devices as per the comments there. The answer posted by Alok Save https://stackoverflow.com/users/452307/alok-save , suggests using the applyBatch() method as follows:

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE, null)
   .withValue(RawContacts.ACCOUNT_NAME,null )
   .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
   .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
   .build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
   .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
   .build());  

ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM