使用phonegap,我可以从联系人列表中获取/筛选单个联系人。但是如何更新(添加/删除)电话号码字段。请帮忙。非常感谢。
比方说,1有一个联系人名字John Smith,有2个电话号码[("家","1111"),("工作","2222")]。
- 当我试图删除"工作"号码时,只保留"家庭"号码。首先获取联系人,尝试删除所有号码,然后添加"家庭"号码,但我总是同时获得3个号码[("家庭","1111"),("工作","2222")
- 奇怪的是,如果我试图删除所有号码,然后什么都不加,那真的会删除联系人的所有号码吗
这是我的代码
var phoneNumbers = [];
for (...){
phoneNum = {
type: ...,
value: ...,
pref: false
};
phoneNumbers.push(phoneNum);
}
contact = contacts_list[index]; //get the contact need to edit
//try to remove all current phone number
if (contact.phoneNumbers){
for (var i = 0; i < contact.phoneNumbers.length; i++){
delete contact.phoneNumbers[i];
//contact.phoneNumbers[i] = null; //i try this too
//contact.phoneNumbers[i] = []; //i try this too
}
}
//set new phone number
contact.phoneNumbers = phoneNumbers;
contact.save(...)
我还尝试创建一个只有1个数字[('Home','1111')]的新联系人,将id和rawId设置为与我需要更新的联系人对象相同,然后保存()。但我仍然得到相同的结果[('Home','1111'),('Work','2222'),('Home','11111')]
var contact = navigator.contacts.create();
var phoneNumbers = [];
phoneNumbers[0] = new ContactField('Home', '1111', false);
contact.phoneNumbers = phoneNumbers;
contact.id = ...
contact.rawId = ...
contact.save(...);
这也是
contact = contacts_list[index]; //get the contact need to edit
//try to remove all current phone number
if (contact.phoneNumbers){
for (var i = 0; i < contact.phoneNumbers.length; i++){
delete contact.phoneNumbers[i];
//contact.phoneNumbers[i] = null; //i try this too
//contact.phoneNumbers[i] = []; //i try this too
}
}
var phoneNumbers = [];
phoneNumbers[0] = new ContactField('Home', '1111', false);
contact.phoneNumbers = phoneNumbers;
contact.save(...)
在cordova的联系人插件中,您可以保存传递原始联系人id的联系人,它会更新数据库中的联系人详细信息。
这里有一个例子:
//Set the options for finding conact
var options = new ContactFindOptions();
options.filter = 'Bob'; //name that you want to search
options.multiple = false;
var fields = ["id","displayName", "phoneNumbers"];
navigator.contacts.find(fields, sucessUpdate, onError, options);
function sucessUpdate(contacts) {
var contact = contacts[0]; //found contact array must be one as we disabled multiple false
// Change the contact details
contact.phoneNumbers[0].value = "999999999";
contact.name = 'Bob';
contact.displayName = 'Mr. Bob';
contact.nickname = 'Boby'; // specify both to support all devices
// Call the "save" function on the object
contact.save(function(saveSuccess) {
alert("Contact successful update");
}, function(saveError){
alert("Error when updating");
});
}
function onError(contactError)
{
alert("Error = " + contactError.code);
}