Search code examples
androidjsonlistviewcustom-adapter

JSON response not printed properly in android Listview using volley


I have a json response which i printed in a listview.It is printed the values from json but all value not printed correctly.Some json values are printed in wrong place.

JSON Response:

```
[
   {
      "HouseNo":"33333333",
      "AreaName":"ghfhgfhg",
      "Landmark":"",

   },
   {

      "HouseNo":"33333333",
      "AreaName":"gfhgfh",
      "Landmark":"",

   }
]

```

This is the code for fetching the specific value:

  StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();




                            try {
                                JSONArray jsonarray = new JSONArray(response);
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    JSONObject obj = jsonarray.getJSONObject(i);
                                Customer customer = new Customer();
                                customer.setTitle(obj.getString("HouseNo"));
                                customer.setSerial(obj.getString("AreaName"));
                                customer.setService(obj.getString("Landmark"));

                                customerList.add(customer);

                            }

                        } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                         adapter.notifyDataSetChanged();
                                  }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        })

some response are printed ok; But in place of some response printed printed other response.How to resolve this issue?


Solution

  • Your json parsing code is right.Try to override add() method in adapter class if your adapter extends to ArrayAdapter class.

    @Override
        public void add(Customer object) {
            customerList.add(object);
            super.add(object);
        }
    

    This will automatically updates your listview.

    And If you are using the BaseAdapter.Then there might be error in Model class or List object used for adapter.