问题描述
我尝试使用以下方法更新特定联系人的多个电话号码代码:
I am tried to update multiple phone numbers of specific contact using followingcode:
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);
使用此代码,我尝试更新三个数字,但仅更新"TYPE_FAX_HOME"号码已更新,另外两个号码已从联系人中删除.
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.
请帮助我.
推荐答案
我从您的代码中了解到,您将相同电话号码值用于这三种类型.因此,在显示时,android将仅显示其中的 1 个联系人.但是,如果您实际上编辑该联系人,则可以看到,所有3种类型均填充了相同的编号.
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:我假设您要为其进行编辑的联系人已经为所有3种类型填充了一些号码.如果没有,请创建它们,然后尝试运行您的代码.
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.
这篇关于更新联系人中的多个电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!