Setting Json Response In Listview
if (status == 200) { String result = EntityUtils.toString(response.getEntity()); Log.d('Response', result); ArrayAdapter < String > adapter = new ArrayAdapter <
Solution 1:
How would i set my name parameter in the listView.
At a glance. Third parameter of ArrayAdapter
should be collection but you are passing only simple String. According to your JSON
i recommend you:
- Create own object that will represent Person with properties(name, age, etc.)
- Parse
JSON
and create collection of persons - Set collection to ListAdapter
Example:
publicclassPerson {
privateString name;
...
// getters and setters@OverridepublicStringtoString() {
returnthis.name;
}
}
Then, you need to parse JSON
and get proper data:
List<Person> persons = newArrayList<Person>();
Person person = null;
JSONObject root = newJSONObject(sourceString);
JSONArray appointments = root.getJSONArray("Appointments");
JSONObject child = null;
for (int i = 0; i < appointments.length(); i++) {
child = appointments.getJSONObject(i);
if (child != null)
person = newPerson();
person.setName(child.getString("Name"));
persons.add(person);
}
}
Then initialise ListAdapter
:
newArrayAdapter<String>(getActivity(), layout, persons);
and now you got it.
Note:
Reason why i'm overriding toString()
method is that since you are using default ArrayAdapter with build-in Android layout each object in your List will be converted to String and will be shown in ListView but if i don't override toString()
it won't return person's name but string representation of object that is human-unreadable string.
Solution 2:
result
is a String containing the entire response. You need to parse or split that string into the items that you want.- You need to give your adapter the items (either as a list or an array). Right now you've created an adapter but haven't provided it any data via the constructor or any setter.
I suggest you read more about ListViews and Adapters on the Android developers site.
Post a Comment for "Setting Json Response In Listview"