Franksye Sipangkar
Franksye Sipangkar

Reputation: 139

JSON Parsing to ListView Android

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"
}, {
    "area": "Puri",
    "city": "Jakarta"
}, {
    "area": "Grogol",
    "city": "Jakarta"
}]

and I want to make ListView like this :

enter image description here

Thank you very much.

Upvotes: 1

Views: 8394

Answers (3)

saurabh
saurabh

Reputation: 1

make changes in ur layoutxml file take two text boxes beside this will work

Upvotes: 0

Cgx
Cgx

Reputation: 763

Remaining on your own

try {
        ArrayList<User> list = new ArrayList<>();
        JSONArray array = new JSONArray(url);
        for (int i = 0; i <array.length() ; i++) {
            JSONObject jsonObject = array.optJSONObject(i);
            String area = jsonObject.optString("area");
            String city = jsonObject.optString("city");
            list.add(new User(area,city));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

class User {
    String area;
    String city;
    public User(String area,String city){
        this.area=area;
        this.city=city;
    }

Upvotes: 0

Jack Ryan
Jack Ryan

Reputation: 1318

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
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<String>(this,R.layout.ListView,StringArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

Upvotes: 1

Related Questions