PARSing JSON to ListView

前端 未结 3 1829
别那么骄傲
别那么骄傲 2021-01-15 11:00

Okay I have went through tons of examples on PARSing my JSON results, with no luck. I have the below JSON example I don\'t want status info or geoLocation right now. I just

3条回答
  •  天涯浪人
    2021-01-15 11:41

    Here's an example of how you would parse the array and display it in the listview. I didn't include the xml but it is pretty simple to describe the listview and multi-line result containing the txt fields for country and address (or whatever):

     String[] from = new String[] {"row_1", "row_2"};
     int[] to = new int[] { R.id.country, R.id.address};
     List> fillMaps = new ArrayList>();
    
    try {
         JSONObject obj = new JSONObject(jsonString);
         JSONArray stations = obj.getJSONArray("stations");
         Log.i(TAG,"Number of entries " + stations.length());
         for (int j = 0; j < stations.length(); j++) {
                 JSONObject jsonObject = stations.getJSONObject(j);
                 HashMap map = new HashMap();
                 map.put("row_1", jsonObject.getString("country"));
                 map.put("row_2", jsonObject.getString("address"));
    
                 fillMaps.add(map);
         }
     } catch (Exception e) {
            e.printStackTrace();
     }
    
     SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
     mListView.setAdapter(adapter);
    

提交回复
热议问题