2012-05-23 31 views
0

它正在連接並從json數組中提取數據。它添加了兩個listview項目但沒有顯示任何文本?爲什麼數據不會顯示在我的列表視圖中?

JSON陣列 { 「比賽」:[{ 「標題」: 「我的標題」, 「聯繫方式」: 「我的聯繫人」, 「說明」: 「我的描述」, 「城市」: 「我的城市」, 「國家」:「堪薩斯州」,「類別」:「輪盤」,{「標題」:「我的標題」,「聯繫人」:「我的聯繫人」,「描述」:「我的描述」,「城市」我的城市」, 「國家」: 「康涅狄格」, 「類別」: 「三張牌」}]}

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

     } 
    }); 
} 

}

+0

更新爲什麼這個傢伙工作和我的不是?我們使用相同的示例代碼http://stackoverflow.com/questions/7155322/parse-json-data-into-listview – user1411823

回答

1

嘗試刪除map.put("id", String.valueOf(i));

希望它運行。

EDITED

你的錯誤是在這裏

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

應該

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

由於這是關鍵,這將在SimpleAdapter匹配。 像這樣從每個字符串值中刪除:

所以請確保兩個鍵完美匹配。

+0

它運行它只是沒有顯示文字。沒有例外或任何東西 – user1411823

+0

@ user1411823:看看編輯部分 – Bhavin

0

發佈您的Xml代碼,您是否使用任何定製列表視圖?如果這意味着爲Textview添加一些Dark顏色。

+0

沒有默認文本 – user1411823

1

假設JSON字符串解析是沒有任何例外完成,我認爲你應該使用這樣的結構:

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

欲瞭解更多信息有關去developers guide

+0

不明白。它應該按照它的方式工作嗎? – user1411823

相關問題