java - 为什么数据没有显示在我的 ListView 上?

标签 java android eclipse json listview

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

JSON 数组 {“锦标赛”:[{“标题”:“我的标题”,“联系人”:“我的联系人”,“描述”:“我的描述”,“城市”:“我的城市”,“州”:“堪萨斯州” ,"Category":"轮盘"},{"Title":"我的头衔","Contact":"我的联系人","Description":"我的描述","City":"我的城市","State":"康涅狄格州","类别":"三张牌扑克"}]}

还有我的一些 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(); 

        }
    });
}

}

最佳答案

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

希望它能运行。

已编辑

你的错误就在这里

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

应该是

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

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

因此,请确保两个 key 完全匹配。

关于java - 为什么数据没有显示在我的 ListView 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714485/

相关文章:

java - 无法使用 JAXB 读取 XML 文件

java - 事务在使用 OpenJPA 时处于非 Activity 状态

android - AWB 不自信

java - 系统找不到Eclipse(Web项目)中指定的路径

java - eclipse ide 丢失源文件目标

java - 如何创建实用程序类?

java - 贾斯珀 : Compiling same jrxml file into multiple output formats

java - gridview 中的图像在滚动时改变其位置

android - 无法使用 React Native 和 Firebase 进行 Google OAuth 身份验证

Eclipse 无法在 Ubuntu 上获取 key 输入