Devesh Agrawal
Devesh Agrawal

Reputation: 9212

Listview is not getting updated immediately after button click

I have a list view that contains Product (productid, productname) + one button

I am filling ArrayList below.

ArrayList<Product> products = new ArrayList<Product>();
//fill products here
productAdapter adapter = new ProductAdapter(getApplicationContext(), products);
productListView.setAdapter(adapter);
productListView.setVisibility(View.VISIBLE);

Now in adapter

public View getView(final int position, View convertView, ViewGroup parent) {

// some other code

    holder.btn_addtocart.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        products.get(position).setname("ABC");

       }
    });
}

Problem is its not updating immediately. When we scroll down and up its getting changed.

Why is not not updating listview immediately?

How to fix this?

Upvotes: 0

Views: 98

Answers (1)

Rami
Rami

Reputation: 7929

To refresh the list, you need to call notifyDataSetChanged() after updating the data.

eg:

holder.btn_addtocart.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          products.get(position).setname("ABC");
          notifyDataSetChanged();
       }
});

Upvotes: 1

Related Questions