【发布时间】:2020-01-05 06:35:10
【问题描述】:
我想在 listview 上可视化 Json 数据,但我不知道该怎么做......我尝试使用 TextView 来验证数据的正确通道,它似乎可以工作,但我是否需要在 listView 上显示它们...想法?
{"Esito":true,"Dati":[{"id":"357","id_utente":"16","nome_prodotto":"cozze"},{"id":"358","id_utente":"16","nome_prodotto":"riso"},{"id":"362","id_utente":"16","nome_prodotto":"patate"},{"id":"366","id_utente":"16","nome_prodotto":"cozze"},{"id":"367","id_utente":"16","nome_prodotto":null}]}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.G[enter image description here][1]ET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Dati");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dato = jsonArray.getJSONObject(i);
String id = dato.getString("id");
String id_utente = dato.getString("id_utente");
String nome_prodotto = dato.getString("nome_prodotto");
mTextViewResult.append(id + ", " + id_utente + ", " + nome_prodotto + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
使用回收器视图而不是列表视图并检查将数据解析到回收器视图的示例...
-
创建一个模型类,并将你的 JSON 解析到其中。然后遍历您的 JSONArray 并将您的模型收集到列表中。然后将此列表设置为 RecyclerView 的来源。
-
请分享 JSON 格式
标签: android json listview android-listview