【问题标题】:How parse JSON data into ListView如何将 JSON 数据解析为 ListView
【发布时间】: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


【解决方案1】:

只需创建新的对象类并收集数据以列出:

class YourObiekt {
private String id;
private String idUtente;
private String nomeProdotto;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getIdUtente() {
    return idUtente;
}

public void setIdUtente(String idUtente) {
    this.idUtente = idUtente;
}

public String getNomeProdotto() {
    return nomeProdotto;
}

public void setNomeProdotto(String nomeProdotto) {
    this.nomeProdotto = nomeProdotto;
}

}

 List<YourObiekt> yourObiektList = new ArrayList<YourObiekt>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, 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);
                        YourObiekt yo = new YourObiekt();

                        yo.setId(dato.getString("id"));
                        yo.setIdUtente(dato.getString("id_utente"));
                        yo.setNomeProdotto(dato.getString("nome_prodotto"));

                        yourObiektList.add(yo);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

现在您将您的ObiektList 作为您的listView 的数据

【讨论】:

  • 嗨,我听从了你的建议,但我不明白如何在列表视图中输入数据......我这样做了:lstView = findViewById(R.id.lstView); ArrayAdapter 适配器 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, yourObiektList); lstView.setAdapter(适配器);
猜你喜欢
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多