无法删除联系人,android phonegap

时间:2013-12-12 07:22:52

标签: android cordova android-contacts contactscontract

我正在使用phonegap-3.1.0中的android应用程序 我想在我的应用程序中使用手机通讯录,所以我推荐了Document.

成功安装了联系人插件

当我删除已保存的联系人(从javascript代码保存)时,它会提醒删除成功 但是当我进入联系人时,它仍然没有从这里删除,

每次尝试时都会保存联系人,但不会因为删除成功等提醒而删除,

我应该怎么做...... 所以我需要帮助,为什么联系人无法删除

1 个答案:

答案 0 :(得分:0)

我为联系人插入和删除 Sample app

创建了一个应用
  

你可以在github上分叉 - > xxbinxx/phoneGap-ContactsApp-Android。你绝对可以解决你的问题。    我已将联系人ID用于删除目的。   这是短代码......

    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
}

希望这会对你有所帮助。 :)

相关问题