2013-07-15 30 views
2

我正在使用phonegap示例應用程序列出所有聯繫人的名字,但我無法這樣做。 以下是我的onSuccess()函數。我得到在此函數的第二行中找到7個聯繫人作爲消息。我通過我的試驗顯示了一些結果。使用電話顯示聯繫人列表

function onSuccess(contacts) { 
    navigator.notification.alert('Found ' + contacts.length + ' contacts.'); 
    var con = document.getElementById('con'); 

    for (var i = 0; i < contacts.length; i++) { 
     navigator.notification.alert(contacts[i].name); 
    } 
}; 

試驗:

<br/> 
    1). navigator.notification.alert(contacts[i])<br/> 
    Result -> 
    {"id":"0","displayName":"Andrew Hill","nickname":"Andrew Hill", 
    "phoneNumbers":["(206)5550001"],"emails":["[email protected]=urthcoffee.com"], 
    "addresses":["Microsoft.Phone.UserData.ContactAddress"], 
    "urls":["www.fourthcoffee..com"]} 
    and like in the same foramt for other 6 contacts. 

    2). navigator.notification.alert(contacts[i].name)<br/> 
    Result -> 
    message 
    like same for all. 

    3). navigator.notification.alert(contacts[i].name[i].value)<br/> 
    Result -> 
    shows nothing. 

請幫我在獲取名稱字段。在此先感謝

+0

hi ios/Android?其中phonegap版本plz –

+0

@ArjunTRaj我正在使用Visual Studio 2010的Window Phone 7 –

回答

6

那麼,我還沒有測試過的Windows Phone 7,但它與iOS/Android的工作正常,測試設備和模擬器。

navigator.contacts.find(
    ['displayName', 'name','phoneNumbers'], 
    function(contacts){ 
     var contact_name; 
     var contact_phone; 
     for(i = 0; i < contacts.length; i++) { 
      if(contacts[i].name.formatted != null && contacts[i].name.formatted != undefined) { 
       contact_name = contacts[i].name.formatted; 
       contact_name = contact_name.replace(/'/g,"''"); 
       if(contacts[i].phoneNumbers != null && contacts[i].phoneNumbers.length > 0 && contacts[i].phoneNumbers[0].value != null && contacts[i].phoneNumbers[0].value != undefined) { 
        console.log(contacts[i].phoneNumbers[0].value); 
        contact_phone = contacts[i].phoneNumbers[0].value; 
       } else { 
        console.log("--No Number-"); 
        contact_phone = ""; 
       } 
      } 
     } 
    },function(error){ 
     alert(error); 
    },{ filter:"", multiple:true } 
); 
相關問題