Problem adding contacts informations
I'm trying to add some contacts from an xml file which have been serialize with Simple xml framework and there is a weird error :
ERROR/ContentProviderOperation(10727): mType: 1, mUri: content://com.android.contacts/data, mSelection: null, mExpectedCount: null, mYieldAllowed: false, mValues: data1=Karl Koffi Marx Antoine Carter mimetype=vnd.android.cursor.item/name, mValuesBackReferences: raw_contact_id=1, mSelectionArgsBackReferences: null
This is the code
ContactList contactList = serializer.read(ContactList.class, xmlFile);
int nbreContacts = contactList.contact.length;
for(int i=0;i<nbreContacts;i++)
{
ArrayList<ContentProviderOperation开发者_开发百科> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
id = contactList.contact[i].getId();
name = contactList.contact[i].getName();
addName(Integer.parseInt(id), name);
flush(c);
}
private void addName(int contactId, String displayName)
{
if(displayName != null)
{
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, contactId)
.withValueData.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
.build());
}
}
private void flush(Context c)
{
ContentResolver cr = c.getContentResolver();
try
{
cr.applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (RemoteException e)
{
Log.e("Writing", "Remote Error writting data ", e);
}
catch (OperationApplicationException e)
{
Log.e("Writing", "OAE Error writting data", e);
}
}
Any help would be appreciate.
Thanks to @Reno on the chat room.
withValueBackReference(Data.RAW_CONTACT_ID, contactId)
changed to .withValueBackReference(Data.RAW_CONTACT_ID, 0)
I first thought RAW_CONTACT_ID was refering to the contact id that's why I was wrong. Problem solved and hope that will help someone else :)
精彩评论