问题
I am tried to update multiple phone numbers of specific contact using following code:
for(int j=0;j<allPhoneNumbersLength;j++)
{
PhoneInfo phoneInfo = (PhoneInfo) allPhoneNumbers.elementAt(j);
String phoneValue = phoneInfo.getValue();
int phoneType = phoneInfo.getIndex(); // phoneType = Phone.TYPE_HOME, Phone.TYPE_WORK, etc
ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?", new String[]{String.valueOf(contactID), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
if(phoneType == Phone.TYPE_HOME)
{
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, Phone.TYPE_HOME);
}
else if(phoneType == Phone.TYPE_WORK)
{
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, Phone.TYPE_WORK);
}
else if(phoneType == Phone.TYPE_FAX_HOME)
{
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, Phone.TYPE_FAX_HOME);
}
op_list.add(builderPhone.build());
}
getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
Using this code I am trying to update three numbers, but only "TYPE_FAX_HOME" number is updated and other two numbers are removed from contact.
Please help me.
回答1:
I've learnt from your code that, you're using the same phone number value for all the 3 types. Hence, while displaying, android will display only 1 of them for the contact. But if you actually edit the contact, there you can see that, all the 3 types have been populated with the same number.
P.S: I'm assuming that the contact for which you're trying to do the edit, already has some number populated for all the 3 types. If not, please create them and then try running your code.
回答2:
Wow ... ! why you use the "if" statement ?! your code could be like this :
for(int j=0;j<allPhoneNumbersLength;j++)
{
PhoneInfo phoneInfo = (PhoneInfo) allPhoneNumbers.elementAt(j);
int phoneType = phoneInfo.getIndex(); // phoneType = Phone.TYPE_HOME, Phone.TYPE_WORK, etc
ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?", new String[]{String.valueOf(contactID), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, phoneType);
op_list.add(builderPhone.build());
}
getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
回答3:
I'm not Android programmer but i think the problem is in last line of code , the loop update the contact info in each step but not apply it , at the end step of the loop apply update and the last changes should be apply , if you change your code like this i think it should work , it means for each loop's step the contact info will update :
for(int j=0;j<allPhoneNumbersLength;j++)
{
PhoneInfo phoneInfo = (PhoneInfo) allPhoneNumbers.elementAt(j);
int phoneType = phoneInfo.getIndex(); // phoneType = Phone.TYPE_HOME, Phone.TYPE_WORK, etc
ContentProviderOperation.Builder builderPhone = ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?"+" AND "+ContactsContract.Data.MIMETYPE + "=?" + " AND "+Phone.TYPE+"=?", new String[]{String.valueOf(contactID), Phone.CONTENT_ITEM_TYPE, String.valueOf(phoneType)});
builderPhone.withValue(Phone.NUMBER, phoneValue)
.withValue(Phone.TYPE, phoneType);
op_list.add(builderPhone.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list);
}
回答4:
Problem with Android Emulator. Code works fine in real device. Thanks for all your help.
来源:https://stackoverflow.com/questions/14785210/update-multiple-phone-numbers-in-contact