简体   繁体   English

为什么我的列表视图中没有显示数据?

[英]Why isn't the data showing up on my listview?

It's connecting and pulling the data from the json array. 它连接并从json数组中提取数据。 Its adding two listview items but not showing any text? 它添加了两个listview项但没有显示任何文本?

JSON array {"tournaments":[{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":"Kansas","Category":"Roulette"},{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":"Connecticut","Category":"Three Card Poker"}]} JSON数组{“锦标赛”:[{“标题”:“我的标题”,“联系方式”:“我的联系人”,“描述”:“我的描述”,“城市”:“我的城市”,“州”:“堪萨斯州“,”类别“:”轮盘“},{”标题“:”我的头衔“,”联系方式“:”我的联系人“,”描述“:”我的描述“,”城市“:”我的城市“,” State“:”Connecticut“,”Category“:”Three Card Poker“}}}

And some of my code in java... 我的一些代码在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(); 

        }
    });
}

} }

Assuming that JSON string parsing is completed without any exceptions, I think that you should use this construction: 假设JSON字符串解析没有任何异常,我认为你应该使用这种结构:

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

For more info about go to developers guide 有关转到开发人员指南的更多信息

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

Hope it runs then. 希望它能够运行。

EDITED EDITED

Your Mistake is Here 你的错误就在这里

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

it should be 它应该是

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

As it is Key which will be Matched in SimpleAdapter. 因为它是将在SimpleAdapter中匹配的Key。 Like this remove : from each string value. 像这样删除:从每个字符串值。

So please make sure that both the Keys match perfectly. 所以请确保两个键完全匹配。

Post your Xml code, are you using any Customized Listview? 发布您的Xml代码,您使用任何自定义列表视图吗? If it is means add some Dark color for Textview. 如果是意味着为Textview添加一些深色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM