添加联系信息时出现问题

时间:2011-09-24 12:21:32

标签: android contacts

我正在尝试添加一些xml文件中的联系人,这些联系人已经使用Simple xml框架进行序列化,并且有一个奇怪的错误:

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

这是代码

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);
    }
}

任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

感谢@Reno在聊天室。

withValueBackReference(Data.RAW_CONTACT_ID, contactId)已更改为.withValueBackReference(Data.RAW_CONTACT_ID, 0) 我首先想到RAW_CONTACT_ID正在引用联系人ID,这就是我错的原因。问题解决了,希望能帮助别人:)