0

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

JSON 数组 {"tournaments":[{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":" Kansas","Category":"Roulette"},{"Title":"my title","Contact":"我的联系人","Description":"我的描述","City":"我的城市"," State":"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(); 

        }
    });
}

}

4

3 回答 3

1

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

setListAdapter(new ArrayAdapter<String>(Context context, int resource, int textViewResourceId, List<T> objects);

有关转到开发人员指南的更多信息

于 2012-05-23T06:28:57.930 回答
1

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

希望它可以运行。

已编辑

你的错误在这里

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

它应该是

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

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

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

于 2012-05-23T06:33:29.517 回答
0

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

于 2012-05-23T06:24:46.137 回答