2016-05-26 12 views
0

Ich möchte eine erweiterbare Liste mit einem JSONArray machen. Die App funktioniert aber nicht perfekt. Ich denke, ich habe Probleme mit der for() Anweisung. Hier ist der Code:Android: JSONArray in ExpandableList Problem

listDataHeader = new ArrayList<>(); 
listDataChild = new HashMap<>(); 
String json_string; 

try { 
    json_string = json; 

    jsonObject = new JSONObject(json_string); 
    jsonArray = jsonObject.getJSONArray("server_response"); 
    if (jsonArray.length() > 0) { 
     object = jsonArray.getJSONObject(0); 
     for (int i = 0; i < object.length(); i++) { 
      //Check with Log.e 
      Log.e(TAG, "Key = " + object.names().getString(i) + " value = " + object.get(object.names().getString(i))); 
      /*** Working with ExpandableList ***/ 
      listDataHeader.add(object.names().getString(i)); 
      List<String> list = new ArrayList<>(); 
      list.add((object.get(object.names().getString(i))).toString()); 
      listDataChild.put(listDataHeader.get(i), list); 
     } 

     listAdapter = new ExpandableListAdapter(getContext(), listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 
    } else { 
     System.out.println("Empty JSON"); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

Und es gibt den JSON-String:

{ 
"server_response": [{ 
    "Medii Vizuale De Programare": "10", 
    "Retele De Calculatoare": "8", 
    "Tehnici Avansate De Programare": "9", 
    "Tehnologii Web": "9", 
    "Baza De Date": "9", 
    "Programare Functionala": "" 
}, { 
    "Medii Vizuale De Programare": "8", 
    "Retele De Calculatoare": "5", 
    "Tehnici Avansate De Programare": "6", 
    "Tehnologii Web": "7", 
    "Baza De Date": "10", 
    "Programare Functionala": "8" 
}]} 

Also, das Problem ist, wenn ich die Werte sehen will, dann nur das erste Objekt gezeigt werden, die zweite nicht . Irgendeine Idee?

+0

was ist Ihre Header und Inhalt dieser Antwort !!? – Nisarg

Antwort

3

Sie auf einem JsonObject sind Looping, müssen Sie eine Schleife auf dem JsonArray

if (jsonArray.length() > 0) { 
    for (int i = 0; i < jsonArray.length(); i++) { 
     object = jsonArray.getJSONObject(i); 
     Log.e(TAG, "Key = " + object.names().getString(i) + " value = " + object.get(object.names().getString(i))); 
     /*** Working with ExpandableList ***/ 
     listDataHeader.add(object.names().getString(i)); 
     List<String> list = new ArrayList<>(); 
     list.add((object.get(object.names().getString(i))).toString()); 
     listDataChild.put(listDataHeader.get(i), list); 
    } 
    : 
    : 
}//if array >0 
+0

es funktioniert nicht. jetzt sind nur diese zwei Elemente sichtbar: Medii Vizuale De Programare ":" 10 ", " Rechele De Calculatoare ":" 5 " –

+0

@ LászlóCsiki es ist offensichtlich, dass Sie JSON-Struktur, JsonArray, JsonObject nicht verstehen, das ist korrekte Ausgabe Als das JSON, das du zeigst, ist ein Array von 2 Objekten, wenn du jede Eigenschaft in jedem Objekt lesen möchtest, das ist eine andere Geschichte. – Yazan

+0

Ja, es ist obvius, aber ich will es verstehen. Kannst du mir helfen? –