1

我已经按照电话差距网站上的文档编写了代码

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
    // find all contacts
    var options = new ContactFindOptions();

    var fields = ["displayName", "name"];
    navigator.contacts.find(fields, onSuccess, onError, options);
}

// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
    for (var i=0; i<contacts.length; i++) 
    {
        console.log("Display Name = " + contacts[i].displayName);
      var element = document.getElementById('cont');

      element.innerHTML='<li><a href="#">' + contacts[i].displayName + '</a></li>';

    }
}

// onError: Failed to get the contacts
//
function onError(contactError) {
    alert('onError!');
}

</script>`

它在安卓模拟器中运行良好,但是当我安装应用程序三星银河手机时,它返回一个“null”。请帮助。

4

3 回答 3

2

奇怪...我也得到了null...(使用Phonegap 2.0版)

我建议你使用类似的东西contacts[i].name.formatted而不是contacts[i].displayName.


因此,例如,通过使用contacts[i].name.formatted,您的代码应如下所示:

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
    // find all contacts
    var options = new ContactFindOptions();

    var fields = ["displayName", "name"];
    navigator.contacts.find(fields, onSuccess, onError, options);
}

// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {

    var element = document.getElementById('cont'); 
    for (var i=0; i<contacts.length; i++) 
    {
        console.log("Display Name = " + contacts[i].name.formatted);                
        element.innerHTML='<li><a href="#">' + contacts[i].name.formatted + '</a></li>';    
    }
}

// onError: Failed to get the contacts
//
function onError(contactError) {
    alert('onError!');
}


以下是您可以获得的不同信息contacts[i].name

  • contacts[i].name.givenName: 提供给定的名称

  • contacts[i].name.formatted:提供格式化的名称(名字+姓氏)

  • contacts[i].name.middleName: 提供中间名

  • contacts[i].name.familyName:提供姓氏(名字+姓氏)

  • contacts[i].name.honorificPrefix: 提供敬语前缀

  • contacts[i].name.honorificSuffix: 提供敬语后缀

希望这可以帮助。

于 2012-10-17T12:16:52.793 回答
1

这是一个简单的解决方案的复杂问题。只添加了一行代码

 options.filter=""; 
    options.multiple=true;
    var filter = ["*"];

那是完整的代码是

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

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
   // find all contacts
    var options = new ContactFindOptions();
    options.filter=""; 
    options.multiple=true;
    var filter = ["*"];
    navigator.contacts.find(filter, onSuccess, onError, options);


}

// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
    var ele = document.getElementById("cont");
    var str = '<ul data-role="listview" >';
  for (var i=0; i<contacts.length; i++) {


       str=str + '<li>' + contacts[i].displayName + '</li>';
   }
   str = str + '</ul>';

  // ele.innerHTML = str;
}

// onError: Failed to get the contacts
//
function onError(contactError) {
    alert('onError!');
}

它正在工作,顺便说一下,它还根据联系人条目显示一些空值,但它工作正常。

于 2012-10-19T14:54:25.790 回答
0

它可能是 iOS - http://docs.phonegap.com/en/2.1.0/cordova_contacts_contacts.md.html#Contacts。文档说那里不支持 displayName 。我会听从 Littm 的建议,并查看我在此处链接的文档,因为它讨论了哪些属性在不同平台上不起作用。

于 2012-10-17T14:11:23.887 回答