问题
i need help for show array 3 items in dialog
items in array : InvernaderoId, Name and Invernarder
I have a problem with convert jsonobject to jsonarray
JsonObject:
{"data":[{"invernaderoId":"1","name":"inver B","Invernarder":"inv1"},{"invernaderoId":"2","name":"inver C","Invernarder":"inv2"},{"invernaderoId":"3","name":"inv13","Invernarder":"inv3"},{"invernaderoId":"4","name":"inv14 A","Invernarder":"HTFRA14A"}]}
Convert to JsonArray with JSONArray arr = js.getJSONArray("data");
Result example:
[{"invernaderoId":"1","name":"inver B","Invernarder":"inv1"},{"invernaderoId":"2","name":"sd 12 C","Invernarder":"HTESP12C"},{"invernaderoId":"3","name":"s13","Invernarder":"sd"},{"invernaderoId":"4","name":"ds 14 A","Invernarder":"ds"}]
Code the replace array string:
String[] arr2 = arr.toString().replace("},{", " ,").split(" ");
and
final String[] employes= arr2;
Code dialog:
new AlertDialog.Builder(getContext())
.setTitle("Selected one")
.setSingleChoiceItems(employes, itemSelected, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int selectedIndex) {
String nombreselect = employes[selectedIndex];
employes.setText(nombreselect);
}
})
.setPositiveButton("Ok", null)
.setNegativeButton("Cancel", null)
.show();
}
});
Viewer in Dialog select:
回答1:
you can iterate through the JSONArray elements and add them into a String array, then feed your AlertDialg with this String array
final String jsonResponse = "{ \"data\":[ {\n" +
" \"invernaderoId\":\"1\",\n" +
" \"name\":\"inver B\",\n" +
" \"Invernarder\":\"inv1\"\n" +
" \n" +
"},\n" +
" {\n" +
" \"invernaderoId\":\"2\",\n" +
" \"name\":\"inver C\",\n" +
" \"Invernarder\":\"inv2\"\n" +
" \n" +
"},\n" +
" {\n" +
" \"invernaderoId\":\"3\",\n" +
" \"name\":\"inv13\",\n" +
" \"Invernarder\":\"inv3\"\n" +
" \n" +
"},\n" +
" {\n" +
" \"invernaderoId\":\"4\",\n" +
" \"name\":\"inv14 A\",\n" +
" \"Invernarder\":\"HTFRA14A\"\n" +
" \n" +
"}\n" +
" \n" +
"]\n" +
"}";
try {
JSONObject js = new JSONObject(jsonResponse);
JSONArray arr = js.getJSONArray("data");
String[] list = new String[arr.length()];
for (int i = 0; i <= arr.length() - 1; i++) {
JSONObject element = arr.getJSONObject(i);
String InvernaderoId = "\"invernaderoId\":\"" + element.getString("invernaderoId") + "\", ";
String Name = "\"name\":\"" + element.getString("name") + "\", ";
String Invernarder = "\"Invernarder\":\"" + element.getString("Invernarder") + "\"";
list[i] = InvernaderoId + Name + Invernarder;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Title")
.setSingleChoiceItems(list, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
Toast.makeText(getContext(), "Position: " + position, Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("Ok", null)
.setNegativeButton("Cancel", null)
.show();
} catch (JSONException e) {
e.printStackTrace();
}
This is the result
回答2:
String[] arr2 = arr.toString().replace("},{", " ,").split(" ");
since you have an array, the easiest would be to pass the json array properly, create an adapter (EG. ArrayAdapter<DataModel>
) and call setAdapter
on the builder. In the adapter subclass you can control how the single entry is being rendered on the dialog
来源:https://stackoverflow.com/questions/62391076/problem-with-convert-jsonobject-to-string-jsonarray-and-show-in-dialog