I am developing a sync adapter
to sync contacts
in the phone with an ERP system
.
Everything works fine on the emulator, but on the phone I get new phone numbers attached to already existing contacts which were created "by hand".
int rawContactInsertIndex =0;
// Add new items http://techblogon.com/insert-new-contact-in-android-code-example/
for (RestAPIContactParser.Entry e : entryMap.values()) {
rawContactInsertIndex = batch.size();
...
batch.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.SOURCE_ID, e.code)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
rawContactInsertIndex)
.build());
... batch.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, e.phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.build());
when I delete my account contacts, phone numbers which were inserted also gets deleted from the existing contacts. I even created a new contact on the emulator then tried to sync and everything was fine, new phone numbers weren't attached to the old contacts. Any ideas? edited: Its is not clear what is the database structure of the RawContacts table. Shall do something like
rawContactInsertIndex =max(ContactsContract.Data.RAW_CONTACT_ID)+1
and then increment rawContactInsertIndex instead of rawContactInsertIndex = batch.size()?