Search code examples
androidandroid-listview

How to convert JSONArray to ListView in Android?


my json is : {"success":["demo20","demo21","iostest3"],"errorCode":0}

I need to parser json then add value in ListView, here is my code :

 ArrayList<String> items = new ArrayList<String>();
 JSONObject object = new JSONObject(result.toString());
 int errorCode = object.getInt("errorCode");
 if (errorCode == 0){
     JSONArray array = object.getJSONArray("success"); 
      for (int i = 0; i < array.length(); i++){
          ????
       }
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
     listView.setAdapter(arrayAdapter);
 }

 please help me , thanks.

Solution

  • for (int i = 0; i < array.length(); i++){
          items.add(array.getString(i));
    }