PhoneGap-Contacts未加载

时间:2013-12-11 06:06:28

标签: android cordova contacts

我因没有得到所有联系人而遇到麻烦。这是我的代码

<!DOCTYPE html>
<html>
<head>
    <title>Contact Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.9.0.js"></script>
    <!-- jQuery and jQuery Mobile -->
    <script src="jquery-1.9.1.min.js"></script>
    <script src="jquery.mobile-1.3.1.min.js"></script>

    <script type="text/javascript" charset="utf-8">

        document.addEventListener("deviceready", getContactList, false);

        function getContactList() { 
            var contactList = new ContactFindOptions(); 
            contactList.filter=""; 
            contactList.multiple=true;
            var fields = ["*"];  //"*" will return all contact fields
            navigator.contacts.find(fields,  getContactFields, contactList );
        }

        function getContactFields(contacts) {
            for (var i=0; i<contacts.length; i++)
            {
               alert(contacts.length);
               alert("Name:" + contacts[i].displayName + "\n"+
                         "Birthday:"+ contacts[i].birthday)

                for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
                    alert("Type: " + contacts[i].phoneNumbers[j].type + "\n" + 
                        "Value: "  + contacts[i].phoneNumbers[j].value );
                } 
         }  

         alert("loaded");
    }
    </script>

请在我出错的地方帮助我。

2 个答案:

答案 0 :(得分:2)

最后我解决了我的问题。如果任何人需要列出他的电话号码列表,那么这段代码将是有益的。以下是源代码。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title></title>

<link rel="stylesheet" href="jquery.mobile-1.3.1.min.css"> 
<!-- jQuery and jQuery Mobile -->
<script src="jquery-1.9.1.min.js"></script>
<script src="jquery.mobile-1.3.1.min.js"></script> 

<!-- for codova plugins --> 
<script type="text/javascript" charset="utf-8" src="cordova-2.9.0.js">  </script>     

<script type="text/javascript" charset="utf-8">

    document.addEventListener("deviceready", onDeviceReady, false); 
    function onDeviceReady() {
        var options = new ContactFindOptions();
        options.filter = "";           
        options.multiple = true;       
        var fields = ["displayName", "phoneNumbers"];  
        navigator.contacts.find(fields, onSuccess, onError, options);
    } 
    function onSuccess(contacts) {  
        for (var i = 0; i < contacts.length; i++) 
        {  
            if (contacts[i].phoneNumbers)
            {
                for (var j=0; j<contacts[i].phoneNumbers.length; j++) 
                {  
                    $('#contactlist').append(
                    '<li>' + 
                        '<a href="tel:'+ contacts[i].phoneNumbers[j].value +'">' +

                            '<h4>'+ contacts[i].displayName +'</h4>' + 
                            '<h4>'+ contacts[i].phoneNumbers[j].value +'</h4>' + 
                        '</a>' +
                    '</li>'); 
                } 
            }   
        }
        $('#contactlist').listview('refresh');
    }

    function onError(contactError) {
        alert('Error in getting Phone Contacts');
    }

 </script>

</head>
<body> 
  <div id="contactListPage" data-role="page" >

<div data-role="header" data-position="fixed">
    <h1>Contacts</h1>
</div>

<div data-role="content">
     <ul id="contactlist" data-role="listview" data-filter="true"></ul>
</div>      

</div> 
 </body>
</html>

答案 1 :(得分:0)

试试这个 -

document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
    var options = new ContactFindOptions();
    options.filter = "";          // empty search string returns all contacts
    options.multiple = true;      // return multiple results
    var fields = ["displayName", "name"]; // Optional If you want particular fields then use filters 
    navigator.contacts.find(fields, onSuccess, onError, options);
}

function onSuccess(contacts) {

    alert(contacts.length);
    for (var i = 0; i < contacts.length; i++) {
    }
}

function onError(contactError) {
    alert('Error in getting Phone Contacts');
}

关注Link