【问题标题】:How to add Contact List data into PhoneGap ListView如何将联系人列表数据添加到 PhoneGap ListView
【发布时间】:2015-01-17 23:29:39
【问题描述】:

我有一个 javascript 函数,它将读取设备 ContactList 并将它们添加到 javascript 数组中。在我的 HTML 页面中,我采用了 listview。现在根据我的要求,我必须通过以下方式将这些数组数据添加到列表视图中jquery 动态,我无法做到。在启动应用程序时,我无法在手机屏幕上看到任何内容..

这是我从 Mobile 的联系人列表中读取的 javascript 代码..

        function onDeviceReady() {

         // specify contact search criteria
         var options = new ContactFindOptions();
         options.filter="";          // empty search string returns all contacts
         options.multiple=true;      // return multiple results
         filter = ["displayName"];   // return contact.displayName field

          // find contacts
          navigator.contacts.find(filter, onSuccess, onError, options);
         }

         var names = [];

        // onSuccess: Get a snapshot of the current contacts
        //
      function onSuccess(contacts) {
        for (var i=0; i<contacts.length; i++) {
           if (contacts[i].displayName) {  // many contacts don't have displayName
        names.push(contacts[i].displayName);
        }
        }
           alert('contacts loaded');

        } 

这是我的 HTML 列表视图..

<div data-role="page" id="home" data-theme="c">

<div data-role="content">

<div id="header" class="header">
    <h1>Contact Directory</h1>
</div>

    <ul data-role="listview"  id="contactlist" data-theme="a">

    </ul>

</div>
</div>

所以,我的问题是如何通过 jquery 动态地将数组值添加到列表视图中。 谢谢..

【问题讨论】:

    标签: javascript android jquery listview cordova


    【解决方案1】:

    有几种方法,但这是一种方法。

    创建一个简单的字符串变量来保存您的 LI。 遍历名称并附加到字符串

  • + names[x] +
  • ,其中 X 是您的循环计数器。 使用 jQuery 获取 UL dom,然后执行 .html(s),其中 s 是您的字符串。

    基本上,您将

  • ...
  • ...
  • 注入到您的 UL 中。

    最后一步是刷新列表视图,以便 jQuery 正确显示它。这是通过此处定义的刷新 API 完成的:http://api.jquerymobile.com/listview/#method-refresh

    【讨论】:

      猜你喜欢
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      相关资源
      最近更新 更多