开发者

How to put a JSONArray into an Android ListView

开发者 https://www.devze.com 2023-01-16 04:17 出处:网络
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?

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.printStack开发者_如何学JAVATrace();
   } 

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

0

精彩评论

暂无评论...
验证码 换一张
取 消