简体   繁体   中英

How to put a JSONArray into an Android ListView

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)

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM