JSON Parsing to ListView Android

前端 未结 3 820
栀梦
栀梦 2021-01-25 20:02

I have a problem to parsing from JSON to Listview in Android. This is an example of data from JSON :

[{
    \"area\": \"Kebon Jeruk\",
    \"city\": \"Jakarta\"         


        
相关标签:
3条回答
  • 2021-01-25 20:14
    JSONArray arr = new JSONArray(yourJSONresponse);
    List<String> list = new ArrayList<String>();
    for(int i = 0; i < arr.length(); i++){
        String info = arr.getJSONObject(i).getString("area") + arr.getJSONObject(i).getString("city");
        list.add(info);
    }
    

    And then just turn the List into an Array of Strings, and use an adapter to fill the ListView like so:

    ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
    ListView listView = (ListView) findViewById(R.id.listview);
    listView.setAdapter(adapter);
    
    0 讨论(0)
  • 2021-01-25 20:21

    make changes in ur layoutxml file take two text boxes beside this will work

    0 讨论(0)
  • 2021-01-25 20:25

    Remaining on your own

    try {
            ArrayList<User> list = new ArrayList<>();
            JSONArray array = new JSONArray(url);
            for (int i = 0; i <array.length() ; i++) {
                JSONObject jsonObject = array.optJSONObject(i);
                String area = jsonObject.optString("area");
                String city = jsonObject.optString("city");
                list.add(new User(area,city));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
    }
    
    class User {
        String area;
        String city;
        public User(String area,String city){
            this.area=area;
            this.city=city;
        }
    
    0 讨论(0)
提交回复
热议问题