【问题标题】:ArrayList<HashMap<String, String>> has no valuesArrayList<HashMap<String, String>> 没有值
【发布时间】:2012-07-13 08:12:25
【问题描述】:

我的ArrayList&lt;HashMap&lt;String, String&gt;&gt; songslist 中没有任何值。在我的doInBackground 方法中,我有应该填充这个ArrayList 的for 循环。字符串“id”、“interpret”...也有值。问题出在哪里?代码如下:

JSONArray charts = json.getJSONArray(TAG_CHARTS);

                // looping through All Products
                for(int i=0;i<charts.length();i++) {
                    JSONObject json_data = charts.getJSONObject(i);

                    String id = json_data.getString(TAG_ID);
                    String interpret = json_data.getString(TAG_INTERPRET);
                    String titel = json_data.getString(TAG_TITEL);
                    String album = json_data.getString(TAG_ALBUM);
                    String albumcover = json_data.getString(TAG_ALBUMCOVER);
                    String likes = json_data.getString(TAG_LIKES);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_ID, id);
                    map.put(TAG_INTERPRET, interpret);
                    map.put(TAG_TITEL, titel);
                    map.put(TAG_ALBUM, album);
                    map.put(TAG_ALBUMCOVER, albumcover);
                    map.put(TAG_LIKES, likes);       

                    songslist.add(map);

                }

但列表为空。 JSONArray 包含值。

【问题讨论】:

  • 你调试这个???可能是 charts.length() == 0
  • 它没有进入循环。 @Nullpo 是对的。

标签: java json string arraylist hashmap


【解决方案1】:

创建地图并将元素放入列表的代码是可以的。所以它要么

1) songslist 搞砸了。您没有显示其创建位置/方式
2) 你认为charts 里面有物品,但它没有。

【讨论】:

  • 我将发布完整的方法... ArrayList> 歌曲列表;歌曲列表是在 OnCreate 上方的 ShowCharts 活动中创建的
【解决方案2】:

您没有进入循环。您发布的代码可以。 chart.length() == 0

【讨论】:

  • 我进入循环,当我调试解释时,id 等有值,但只是跳过 hashmap 之后的行..
  • 请在 "songslist.add(map);" 之前输入 "System.out.println(songlist)" 并复制该控制台行。
  • 我不能以这种方式使用 System.out.println(songlist)...我正在为 Android 开发,打印出来似乎有点困难,因为“Log.d”确实适用于(字符串,数组列表)
  • String likes = json_data.getString(TAG_LIKES); Eclipse直接跳转到我的:public void onPostExecute(String file_url) { // dismiss the dialog after getting all products pDialog.dismiss(); // updating UI from Background Thread runOnUiThread(new Runnable() { public void run() { /** * Updating parsed JSON data into ListView * */ LazyAdapter adapter = new LazyAdapter(ShowCharts.this, songslist); list.setAdapter(adapter); } });
  • 好吧……好吧……我发现了问题。如此愚蠢!!我忘了在我的适配器中为我的 id 设置一个位置。所以adpater的值太多了,以至于他不知道如何处理最后一个值... -.-
猜你喜欢
  • 2013-10-05
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2013-05-27
相关资源
最近更新 更多