简体   繁体   中英

How to display json data in a listview?

I would like to view the json data inside a listview ...
I tried to create a new object class and collect data to list, but I think I'm wrong about something...
I tried to use a TextView to verify the correct passage of the data and it seems to work, but I would need to display them on the listView... Do you have any ideas to solve my problem?

    public class LoggedActivity extends AppCompatActivity {

    private RequestQueue mQueue;
    private ListView lstView;
    private TextView mTextViewResult;
    Button buttonParse;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logged);


        lstView = findViewById(R.id.lstView);

        mTextViewResult = findViewById(R.id.text_view_result);
        buttonParse = findViewById(R.id.button_parse);

        Bundle extras = getIntent().getExtras();
        final String id_utente = extras.getString("id");



        mQueue = Volley.newRequestQueue(this);

        buttonParse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse(id_utente);
            }
        });

    }

    private void jsonParse(String id_utente) {

        class Prodotti {
            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;
            }
        }

        final List<Prodotti> listaProdotti = new ArrayList<Prodotti>();

        String url = "http://192.168.1.5/progettoPHP/WebServices/webSrv.php?type=recuperaProdotti&idutente=" + id_utente;


        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);

                                Prodotti prdt = new Prodotti();

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

                                listaProdotti.add(prdt);

                                //mTextViewResult.append(id + ", " + id_utente +  ", " + nome_prodotto +  "\n\n");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        mQueue.add(request);


    }

}
  1. create an entity model from json file use http://www.jsonschema2pojo.org/
  2. use Gson library to fill the model https://github.com/google/gson
  3. now you have a beautiful model with data you can getlist add sort and anything else
  4. for display this list use recyclerView or listView or .... recyclerView https://developer.android.com/guide/topics/ui/layout/recyclerview listview https://developer.android.com/reference/android/widget/ListView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM