【问题标题】:How to load JSON data into Android Listview?如何将 JSON 数据加载到 Android Listview 中?
【发布时间】:2014-11-10 09:04:08
【问题描述】:

我想将 JSON 数据加载到带有字幕和图像的列表视图中(行的左侧)。

我在下面发布了我的示例 JSON 响应代码:

 for (int i = 0; i < contacts.length(); i++) {
    JSONObject obj = contacts.getJSONObject(i);
    Iterator keys = obj.keys();
    while(keys.hasNext()) {

      // loop to get the dynamic key
       String currentDynamicKey = (String)keys.next();

       // store key in an arraylist which is A,B,...
       // get the value of the dynamic key
       JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
       Log.d("Response: ", "> " + currentDynamicKey);



      int jsonrraySize = currentDynamicValue.length();
         if(jsonrraySize > 0) {
             for (int ii = 0; ii < jsonrraySize; ii++) {
                  JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
                  String name = nameObj.getString("name");
                  System.out.print("Name = " + name);
                  //Log.d("Response: ", "> " + name);

                  //store name in an arraylist
             }                    
         }
      }
}

我想显示Tittle : currentDynamicKey 值和Sub Title : Name 字符串值。

【问题讨论】:

标签: android json android-listview


【解决方案1】:

如果您已经拥有数据,并且不一定需要将服务器请求与适配器集成,那么您实际上只需要一个支持 JSONArray 的适配器。 Advanced-Adapters 库中有一个非常好的开源软件。然后只需将 JSONArray 传递给适配器并将适配器设置为ListView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2015-06-22
    • 2015-02-01
    • 1970-01-01
    相关资源
    最近更新 更多