[Solved] Making a listview from JSON array


You are creating an empty list and you’re giving it to the adapter. So, there is not a list to display.

listView = (ListView) findViewById(R.id.lstPublikasi);

lstPublikasi = new ArrayList<Publikasi>();

publikasiAdapter = new PublikasiAdapter(lstPublikasi,getApplicationContext());

You must fill the list when the response come successfully. After that you must give the list to the adapter’s method such as “updateList();”

Here is the sample:

    private void showList() {
        final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, uri, null, new Response.Listener < JSONArray > () {@
            Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    try {
                        lstPublikasi.add(new Publikasi(jsonArray.getJSONObject(i)));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                publikasiAdapter.updateList(lstPublikasi);
            }
        }, new Response.ErrorListener() {@
            Override
            public void onErrorResponse(VolleyError error) {}
        });
    }
    public class PublikasiAdapter extends ArrayAdapter < Publikasi > {
        private List < Publikasi > lstPublikasi;
        private Context mCtx;
        public PublikasiAdapter(List < Publikasi > P, Context c) {
            super(c, R.layout.list_publikasi, P);
            this.lstPublikasi = P;
            this.mCtx = c;
        }@
        Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(mCtx);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_publikasi, parent, false);
            }
            TextView textNama = (TextView) convertView.findViewById(R.id.textNama);
            TextView textDetail = (TextView) convertView.findViewById(R.id.textDetail);
            TextView textStatus = (TextView) convertView.findViewById(R.id.textStatus);
            TextView textPeriode = (TextView) convertView.findViewById(R.id.textPeriode);
            Publikasi publikasi = lstPublikasi.get(position);
            textNama.setText(publikasi.namaJurnal);
            textDetail.setText(publikasi.tipePublikasi);
            textStatus.setText(publikasi.status);
            textPeriode.setText(publikasi.periode);
            return convertView;
        }
        public void updateList(List < Publikasi > mlstPublikasi) {
            lstPublikasi = mlstPublikasi;
            notifyDataSetChanged();
        }
    }

solved Making a listview from JSON array