Hardy
Hardy

Reputation: 1539

Update phone contact (Android) via Phonegap

Using phonegap, I can get/filter a single contact from contact list. But how to update (add/remove) phone number field. Please help. Thanks alot.

Lets say 1 got a contact name John Smith with 2 phone number [('Home', '1111'), ('Work', '2222')].

Here is my code

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

I also try create a new contact with only 1 number [('Home', '1111')], set id and rawId as same as i contact object I need to update, then save(). But i still get the same result [('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(...);

this also

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

Upvotes: 3

Views: 1844

Answers (1)

Fabrice Yopa
Fabrice Yopa

Reputation: 334

In the contact plugin of cordova, you can save the contact passing the original contact id, it will update the contact details in the database.

Here is an example:

//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);
}

Upvotes: 2

Related Questions