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',
Solution 1:
JSONArray arr = newJSONArray(yourJSONresponse);
List<String> list = newArrayList<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:
ArrayAdapteradapter=newArrayAdapter<String>(this,R.layout.ListView,StringArray);
ListViewlistView= (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
Solution 2:
Remaining on your own
try {
ArrayList<User> list = newArrayList<>();
JSONArray array = newJSONArray(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(newUser(area,city));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
classUser {
String area;
String city;
publicUser(String area,String city){
this.area=area;
this.city=city;
}
Post a Comment for "Json Parsing To Listview Android"