0

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)

2 Answers 2

3

Just implemented a JSONArrayAdapter to get JSON data into a ListView. It extends android.widget.SimpleAdapter and works much like its parent class.

It only has one static method that converts a JSONArray to a List<Map<String, String>> that is used to initialize the SimpleAdapter.

import ...

public class JSONArrayAdapter extends SimpleAdapter {

    public JSONArrayAdapter(Context context, JSONArray jsonArray, 
                            int resource, String[] from, int[] to) {
      super(context, getListFromJsonArray(jsonArray), resource, from, to);
    }

    // method converts JSONArray to List of Maps
    protected static List<Map<String, String>> getListFromJsonArray(JSONArray jsonArray) {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        // fill the list
        for (int i = 0; i < jsonArray.length(); i++) {
            map = new HashMap<String, String>();
            try {
                JSONObject jo = (JSONObject) jsonArray.get(i);
                // fill map
                Iterator iter = jo.keys();
                while(iter.hasNext()) {
                    String currentKey = (String) iter.next();
                    map.put(currentKey, jo.getString(currentKey));
                }
                // add map to list
                list.add(map);
            } catch (JSONException e) {
                Log.e("JSON", e.getLocalizedMessage());
            }


        }
        return list;
    }


}

Just use it the same way as built-in Android adapters like SimpleCursorAdapter with a mapping of Strings (keys of your json data) to ints (views of your listview rows).

For more information refer to the documentation of SimpleAdapter.

Sign up to request clarification or add additional context in comments.

Comments

2

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.