【问题标题】:can not remove contacts, android phonegap无法删除联系人,android phonegap
【发布时间】:2013-12-12 07:22:51
【问题描述】:

我正在 phonegap-3.1.0 中开发 android 应用程序 我想在我的应用程序中使用电话联系人,所以我参考了这个Document.

已成功安装联系人插件

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

每次我尝试时,它都会保存联系人但不会被删除,因为像 删除成功

这样的警报

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

【问题讨论】:

    标签: android cordova android-contacts contactscontract


    【解决方案1】:

    我已经创建了一个用于联系人插入和删除的应用

    你可以在 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
    }
    

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

    【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 2014-02-14
    • 2021-06-16
    • 1970-01-01
    • 2021-02-08
    • 2015-08-30
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多