如何将json数据设置为列表视图?

时间:2014-01-09 07:09:14

标签: android android-listview

我尝试将json数据添加到listview.But我不知道如何将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);

}

任何人都可以告诉我如何将此数据设置为列表视图。?

2 个答案:

答案 0 :(得分:0)

试试这个..

你正在做反向 forle循环之前的arraylist <去除 取消 for循环中的HashMap

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();

        }

答案 1 :(得分:0)

谷歌GSON

我会用gson这个。查看the gson project website以了解详细信息。这是对象序列化/反序列化的一个例子:

对象示例

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);  

==&GT; json是{&#34; value1&#34;:1,&#34; value2&#34;:&#34; abc&#34;}

请注意,您无法使用循环引用序列化对象,因为这会导致无限递归。

(反序列化)

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

==&GT; obj2就像obj