views:

122

answers:

1

Hi I am trying to populate a ListView in the android app. I have the data in a JsonArray, But it doesn't work to directly use the JsonArray. Any suggestions How to populate the ListView?

   JSONObject json = null;
   try {
       json = new JSONObject(client.getResponse());
       JSONArray nameArray = json.names();  
       JSONArray valArray = json.toJSONArray(nameArray);

   } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } 

   setListAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, valArray));

I am trying to have "valArray", but it doesn't work. Also I wonder what does valarray contain? (JsonArray, what does it contain)

A: 

ArrayAdapter<String> works with Strings. You can't use it for JSONArray.

I think you must implement custom adapter for list. Try extend one of the ListAdapter's subclasses or google for "custom listview adapter".

JSONArray may contains any mix of objects (JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL).

Sergey Glotov