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?
was ist Ihre Header und Inhalt dieser Antwort !!? – Nisarg