3

使用 phonegap,我可以从联系人列表中获取/过滤单个联系人。但是如何更新(添加/删除)电话号码字段。请帮忙。非常感谢。

假设 1 有一个联系人姓名 John Smith 和 2 个电话号码 [('Home', '1111'), ('Work', '2222')]。

  • 当我尝试删除“工作”号码时,只保留“家庭”号码。首先获取联系人,尝试删除所有号码,然后添加“家”号码,但我总是得到 3 个号码 [('Home', '1111'), ('Work', '2222'), ('Home' , '1111')]
  • 奇怪的是,如果我尝试删除所有号码,然后什么都不添加,它真的会从联系人中删除所有号码吗?

这是我的代码

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', '1111')]

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(...)
4

1 回答 1

2

在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);
}
于 2014-11-06T10:17:40.773 回答