can not remove contacts, android phonegap

911 Views Asked by At

I am working on android application in phonegap-3.1.0 I want to use phone contacts in my application, So I have refer this Document.

Successfully installed the plugins for contacts

When I remove saved contact(save from javascript code), It alerts Removal Success But when I go into the contacts, it is still not removed from here,

Every time I try, it saves the contact but not not removed since alerts like Removal Success,

What should I Do... SO I need help on it, Why the contact can't be remove

1

There are 1 best solutions below

2
On

I have created an app for contacts insert and delete Sample app

You can fork on github-> xxbinxx/phoneGap-ContactsApp-Android. you can definitely solve your issue after it. I have used contact ID's for the deletion purpose. Here's the short code...

    var app ={
/********************SOME OTHER CODE*************************/
        openContacts: function() {
        app.initialize();
        var options = new ContactFindOptions();
        options.filter = "";
        options.multiple = true;
        var fields = ["*"]; //"*" will return all contact fields
        navigator.contacts.find(fields, app.onSuccess, app.onError, options);
    },
// Write contacts in DOM
    onSuccess: function(contacts) {
        var li = '';
        $.each(contacts, function(key, value) {
            if (value.name) {
                $.each(value.name, function(key, value) {
                    if (key === 'formatted') {
                        name = value;
                    }
                });
            }
            if (value.note) {
                note = value.note;
            }
            if (value.id) {
                id = value.id;
            } 
            console.log("id : " + id + "-> name : " + name + " -> note : " + note);
            li += '<li style="text-decoration:none;"><b>Name</b>: ' + name + '<div class="removeIcon pullRight" onclick="app.removeThisContact(\'' + id + '\',\'' + name + '\')">&nbsp;</div><br><b> Note:</b> ' + note + '</li>';
        }); // NOTICE the ID is passed as PARAMETER to remove specific contact.
        $("#contact").html(li);
    },
    onError: function(contactError) {
        alert('onError!' + contactError.code);
    },
    removeThisContact: function(id, name) {
        console.log("removing contact : " + name);      
        options = new ContactFindOptions(); // find the contact to delete
        options.filter.id = id;
        options.multiple = "true";
        var fields = ["displayName", "name"]; // you can take any.. 
        navigator.contacts.find(fields, deleteThis, app.onError, options);

        function deleteThis(contacts) {
            var contact = contacts.pop();
// logging things to troubleshoot.
            console.log('inside deleteThisContact: parameter passed: '+ contacts);
            console.log("popped out:" +contact);
                contact.remove(function() {
                    $('#status-area')
                    .flash_message({
                        text: 'Contact Removed!',
                        how: 'append'
                    });
                    app.openContacts();
                }, null);        
        },
    deleteAllTheContacts: function() {
        var deleteContact = function(contacts) {
            console.log("length = " + contacts.length);
            // No contacts left, stop saving
            if (contacts.length == 0) {
                console.log("All contacts removed");
                return;
            }

            var contact = contacts.pop();


contact.remove(function() {
                deleteContact(contacts);
            }, null);
        };

        navigator.contacts.find(["*"], deleteContact, app.onError, {
            "multiple": true
        });
    },
/********************SOME OTHER CODE*************************/
    }
$.fn.flash_message = function(options) {
     //flash your message
}

Hope this will help you. :)