【问题标题】:Why isn't the data showing up on my listview?为什么我的列表视图中没有显示数据?
【发布时间】:2012-05-23 06:18:07
【问题描述】:

它连接并从 json 数组中提取数据。它添加了两个列表视图项但不显示任何文本?

JSON 数组 {“比赛”:[{“标题”:“我的标题”,“联系人”:“我的联系人”,“描述”:“我的描述”,“城市”:“我的城市”,“州”:“堪萨斯州” ,"类别":"轮盘赌"},{"标题":"我的标题","联系人":"我的联系人","描述":"我的描述","城市":"我的城市","州" :"Connecticut","Category":"三张牌扑克"}]}

还有我的一些java代码...

    JSONObject json = JSONfunctions.getJSONfromURL("http://kleinefrosch.com/retrieve.php");

    try{

        JSONArray  tourneys = json.getJSONArray("tournaments");

        for(int i=0;i<tourneys.length();i++){                       
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = tourneys.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("Title:" , e.getString("Title"));
            map.put("Contact:" , e.getString("Contact"));
            map.put("Description:" , e.getString("Description"));
            map.put("City:" , e.getString("City"));
            map.put("State:" , e.getString("State"));
            map.put("Country:" , e.getString("Country"));
            map.put("Category:" , e.getString("Category"));

            mylist.add(map);            
        }       
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "Title","Contact","Description","City","State","Country","Category" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(JsonActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

        }
    });
}

}

【问题讨论】:

标签: java android eclipse json listview


【解决方案1】:

尝试删除map.put("id", String.valueOf(i));

希望它能够运行。

已编辑

你的错误在这里

map.put("Title:" , e.getString("Title")); 

应该是

map.put("Title" , e.getString("Title"));

因为它是在 SimpleAdapter 中匹配的 Key。 像这样从每个字符串值中删除:

所以请确保两个键完全匹配。

【讨论】:

  • 它运行它只是不显示文本。没有例外或任何东西
  • @user1411823 :查看已编辑部分
【解决方案2】:

假设JSON字符串解析完成,没有任何异常,我认为你应该使用这个构造:

setListAdapter(new ArrayAdapter&lt;String&gt;(Context context, int resource, int textViewResourceId, List&lt;T&gt; objects);

更多信息请到developers guide

【讨论】:

    【解决方案3】:

    发布您的 Xml 代码,您是否使用任何自定义列表视图?如果是意味着为 Textview 添加一些深色。

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2019-08-05
      • 2021-02-14
      相关资源
      最近更新 更多