JSON Parsing to ListView Android

前端 未结 3 825
栀梦
栀梦 2021-01-25 20:02

I have a problem to parsing from JSON to Listview in Android. This is an example of data from JSON :

[{
    \"area\": \"Kebon Jeruk\",
    \"city\": \"Jakarta\"         


        
3条回答
  •  礼貌的吻别
    2021-01-25 20:14

    JSONArray arr = new JSONArray(yourJSONresponse);
    List list = new ArrayList();
    for(int i = 0; i < arr.length(); i++){
        String info = arr.getJSONObject(i).getString("area") + arr.getJSONObject(i).getString("city");
        list.add(info);
    }
    

    And then just turn the List into an Array of Strings, and use an adapter to fill the ListView like so:

    ArrayAdapter adapter = new ArrayAdapter(this,R.layout.ListView,StringArray);
    ListView listView = (ListView) findViewById(R.id.listview);
    listView.setAdapter(adapter);
    

提交回复
热议问题