Sai Si
Sai Si

Reputation: 1

list view is not refreshing even after calling notifyDataSetChanged()

I am trying to refresh listView from Adapter class itself but it isn't refreshing don't know where the mistake is.

public class DeliveryListAdapter extends BaseAdapter
{
DeliveryListAdapter ald;
Context ct;
private List<DeliveryListBean> deliveryListBeans;
SharedPreferences companyName;

public DeliveryListAdapter(Context ct,List<DeliveryListBean> deliveryListBeans)
{
    this.ct=ct;
    this.deliveryListBeans=deliveryListBeans;
}
@Override
public int getCount() {
    return deliveryListBeans.size();
}

private void updateResults(List<DeliveryListBean> dlb)
{
    deliveryListBeans=dlb;
    notifyDataSetChanged();
}
//I skipped code for inflating because it is working fine.
delivered.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String serverURL= PathUrls.pathUrl+"evs_updatedeliverystatus.php?db="+companyName.getString("companyName","")+"&invoiceid="+dlb.getInvoiceNo()+"&deliverystatus=2";
            JsonObjectRequest jsonArrayRequest=new JsonObjectRequest(serverURL, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if (response.length()>0)
                    {
                        ald=new DeliveryListAdapter(ct,deliveryListBeans);
                        VolleyLog.v("update delivery list: %n %s ", response.toString());
                        try{
                            int status = response.getInt("status");
                            if (status==1)
                            {
                                Log.d("updated delivery",response.toString());
                                ald.updateResults(deliveryListBeans);
                                Toast.makeText(ct, "Delivery Successful", Toast.LENGTH_SHORT).show();
                            }
                            else if (status==0)
                            {
                                Toast.makeText(ct, "Delivery Has been failed", Toast.LENGTH_LONG).show();
                            }
                        }catch (JSONException e)
                        {
                         e.printStackTrace();
                        }
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.e("Error DeliveryList:%n %s ", error);
                    Toast.makeText(ct,"NetworkError Not Responding", Toast.LENGTH_SHORT).show();
                }
            });
            //Toast.makeText(ct, "companyName"+companyName.getString("companyName","").toString(), Toast.LENGTH_LONG).show();
            VolleySingleton.getsInstance().getRequestQueue().add(jsonArrayRequest);
        }
    });

I am using setOnclickListner for delivered button to update the status. Status is updating but the list is not refreshing when I click on the delivery button.

Upvotes: 0

Views: 67

Answers (3)

Sai Si
Sai Si

Reputation: 1

I found an alternative way of doing it. I just removed the position from list and the mistake is creating new object ald=new DeliveryListAdapter(ct,deliveryListBeans); you don't have to create any new object. What I did is

if (status==1)
{
Log.d("updated delivery",response.toString());
deliveryListBeans.remove(position);
notifyDataSetChanged();
Toast.makeText(ct, "Delivery Successful", Toast.LENGTH_SHORT).show();
}

Upvotes: 0

Cùi Bắp
Cùi Bắp

Reputation: 78

Why you create new Adapter??

 DeliveryListAdapter   ald=new DeliveryListAdapter(ct,deliveryListBeans);

I think you should just update for deliveryListBeans

and use this method of ADM user.

 private void updateResults(List<DeliveryListBean> dlb) {
deliveryListBeans.clear();
deliveryListBeans.addAll(dlb);
notifyDataSetChanged();

}

I dont' see you use 'response' . You just check status from this var <- It is data from your server

Upvotes: 0

SpiritCrusher
SpiritCrusher

Reputation: 21043

As i have said in comment you messed with reference.See the code below:

private void updateResults(List<DeliveryListBean> dlb) {
    deliveryListBeans=dlb;
    notifyDataSetChanged();
}

Here you are assigning new list to the previous list so reference is changed . The adapter will never get notified cause adapter having previous reference . So the Solution is in two cases: if you want the old data too do as and second if you need only new data:

 private void updateResults(List<DeliveryListBean> dlb) {
    deliveryListBeans.addAll(dlb);
    notifyDataSetChanged();
}


 private void updateResults(List<DeliveryListBean> dlb) {
    deliveryListBeans.clear();
    deliveryListBeans.addAll(dlb);
    notifyDataSetChanged();
}

Upvotes: 1

Related Questions