我正在尝试通过我的应用程序更新本机联系人数据。问题是当我使用地址更新时,地址正在更新为名字和注释以及电话字段。下面的代码是我在应用程序中使用的,如果有任何错误,请纠正我。
public ArrayList<ContentProviderOperation> updateContact(Context context, ContactData contactData){
String selectPhone = ContactsContract.Data.CONTACT_ID + "=?";
String[] phoneArgs = new String[]{contactData.getContactLocalId()};
ArrayList <ContentProviderOperation> ops = new ArrayList <> ();
ops.add(ContentProviderOperation.newUpdate(
ContactsContract.RawContacts.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//------------------------------------------------------ Names
ops.add(ContentProviderOperation.newUpdate(
ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
contactData.getFirstName())
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
contactData.getLastName())
.build());
//------------------------------------------------------ Mobile Number
ops.add(ContentProviderOperation.
newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contactData.getContactPhoneBook().get(0).getNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
//------------------------------------------------------ Email
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, contactData.getEmailbook().get(0).getEmail())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
//------------------------------------------------------ Notes
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Note.NOTE, contactData.getNotes())
.build());
//------------------------------------------------------ Address
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contactData.getAddressbook().get(0).getStreet_line_1())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.STREET)
.build());
// Asking the Contact provider to updating a contact
try {
ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return ops;
}
终于,我找到了问题的答案。
String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "= ?";
String[] nameArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String[] mobileArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
String[] emailArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String[] notesArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
String[] addressArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
我们应该传递单独的更新参数。
public ContactData updateContact(Context context, ContactData contactData){
String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "= ?";
String[] nameArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String[] mobileArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
String[] emailArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String[] notesArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
String[] addressArgs = new String[]{contactData.getContactLocalId(), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
ArrayList <ContentProviderOperation> ops = new ArrayList <> ();
//------------------------------------------------------ Names
ops.add(ContentProviderOperation.newUpdate(
ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, nameArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contactData.getFirstName())
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, contactData.getLastName())
.build());
//------------------------------------------------------ Mobile Number
ops.add(ContentProviderOperation.
newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, mobileArgs)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contactData.getContactPhoneBook().get(0).getNumber())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
//------------------------------------------------------ Email
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, emailArgs)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, contactData.getEmailbook().get(0).getEmail())
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
//------------------------------------------------------ Notes
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, notesArgs)
.withValue(ContactsContract.CommonDataKinds.Note.NOTE, contactData.getNotes())
.build());
//------------------------------------------------------ Address
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(selectPhone, addressArgs)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contactData.getAddressbook().get(0).getStreet_line_1())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.STREET)
.build());
// Asking the Contact provider to updating a contact
try {
ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
String serverId = markAsUpdated(contactData);
contactData.setContactId(serverId);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return contactData;
}