2014-01-09 47 views
0

我試圖添加JSON數據到listview.But我不知道如何添加JSON數據列表適配器。如何將json數據設置爲列表視圖?

try { 
     mylist = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map = new HashMap<String, String>(); 

    JSONObject jsonResponse = new JSONObject(strJson1); 
    JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants"); 



    for (int i = 0; i < jsonMainNode.length(); i++) { 

     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
     String restName = jsonChildNode.optString("name"); 
     Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show(); 
     if(restName.equalsIgnoreCase(name)){//this name is predefined name. 
     String address = jsonChildNode.optString("address"); 
     String mobile = jsonChildNode.optString("mobile"); 
     String direction = "Direction: "+jsonChildNode.optString("direction"); 
     String bestTime = "Best time to visite: "+jsonChildNode.optString("bestTime"); 
     String food = jsonChildNode.optString("food"); 
     String dress = jsonChildNode.optString("dress"); 
     String priceRange = "Price Range: "+jsonChildNode.optString("priceRange"); 
     String rate = jsonChildNode.optString("Rate"); 
     String comment = "Price Range: "+jsonChildNode.optString("comment"); 


     map.put("restName",restName); 
     map.put("address",address); 
     map.put("mobile",mobile); 
     map.put("direction",direction); 
     map.put("bestTime",bestTime); 
     map.put("food",food); 
     map.put("dress",dress); 
     map.put("priceRange",priceRange); 
     map.put("rate",rate); 
     map.put("comment",comment); 

     mylist = new ArrayList<HashMap<String, String>>(); 
     mylist.add(map); 
     }else{ 

      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); 

     } 

    } 


} catch (JSONException e) { 
    Toast.makeText(getApplicationContext(), "Error..." + e.toString(), 
      Toast.LENGTH_LONG).show(); 
} 

// ListAdapter adapter = new SimpleAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,mylist); 
// list.setAdapter(adapter); 

} 

任何人都可以告訴我如何將這些數據設置爲列表視圖。

+0

ü應該使用自定義適配器。 – keshav

+0

你解決了你的問題嗎? @anuruddhika – dipali

+0

你可以在這裏找到答案=> [Android從JSON填充ListView - 示例](http://www.learn2crack.com/2013/11/listview-from-json-example.html) – Yanshof

回答

0

試試這個..

你做反向decleararraylist之前循環declearHashMapfor循環內。

mylist = new ArrayList<HashMap<String, String>>();   

    JSONObject jsonResponse = new JSONObject(strJson1); 
    JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants"); 



    for (int i = 0; i < jsonMainNode.length(); i++) { 

     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
     String restName = jsonChildNode.optString("name"); 
     Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show(); 
     if(restName.equalsIgnoreCase(name)){//this name is predefined name. 
     HashMap<String, String> map = new HashMap<String, String>(); 
     String address = jsonChildNode.optString("address"); 
     //So on 


     map.put("restName",restName); 
     //So on 

     mylist.add(map); 
     }else{ 

      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); 

     } 
+0

是的,我已經添加它在頂部。 – anuruddhika

+0

@anuruddhika然後你再次啓動裏面,如果條件,那麼將再次清除或重新初始化每次循環運行。 – Hariharan

+0

我刪除了你提到的代碼。但它不起作用。 – anuruddhika

0

谷歌,GSON

我會用GSON這一點。請查詢the gson project website瞭解更多詳情。這是對象序列化/ deserialisation一個例子:

對象實例

class BagOfPrimitives { 
    private int value1 = 1; 
    private String value2 = "abc"; 
    private transient int value3 = 3; 
    BagOfPrimitives() { 
    // no-args constructor 
    } 
} 

(串行化)

BagOfPrimitives obj = new BagOfPrimitives(); 
Gson gson = new Gson(); 
String json = gson.toJson(obj); 

==> JSON爲{ 「值1」:1, 「VALUE2」:」 abc「}

請注意,您無法序列化帶有循環引用的對象,因爲這會導致無限遞歸。

(反序列化)

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class); 

==> OBJ2,就像OBJ

相關問題