0

I have a ListView which contains a list of products. Each list item contains a TextView which shows the quantity in the cart, if the product is in the cart.

The cart is a popup and the product quantity can be changed from the cart.

The issue is when I dismiss the cart pop up, the quantity in the list item view is not updated since the getView is not called.

I am aware of PopupWindow.OnDismissListener but dont know how to update the view explicitly or call getView forcibly.

Is there a way I can achieve this? Thank you.

Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87
ashwin mahajan
  • 1,376
  • 3
  • 17
  • 43

2 Answers2

2

So from your question I guess, you're passing a ArrayList of Product in your adapter. So lets just assume the Product class looks like this.

class Product {
    public String productName; 
    public int quantity;
}

Now when you update the quantity from the cart, you need to update the ArrayList which you have passed to the adapter and then call notifyDataSetChanged() on the adapter to see the changes in the list.

So I'm writing some pseudo code for changing the quantity from the cart.

public void changeQuantity(int index, boolean quantityIncreased) {
    if(quantityIncreased) products.get(index).quantity = products.get(index).quantity + 1;
    else  products.get(index).quantity = products.get(index).quantity - 1;
}

Now Override your PopupWindow.OnDismissListener like this. Call notifyDataSetChanged to see the effect in your list.

@Override
public void onDismissListener() {
    adapter.notifyDataSetChanged();
}
Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87
  • I considered calling `notifyDataSetChanged` but it will refresh the whole layout. Just a look at the pop up, the item that user was viewing will be gone, back to where he started. Thats the problem. – ashwin mahajan Jun 23 '17 at 22:25
  • I didn't quite get you. The item that user was viewing will be gone - what did you mean by this? And yes, don't save the intermediate actions taken on the list? – Reaz Murshed Jun 23 '17 at 22:27
  • I meant the list will be reset and will have to go down again to look further. – ashwin mahajan Jun 23 '17 at 22:30
  • 1
    No it should not. And if it does, you can always set the scroll position to the last saved scroll position of you list. In that case, save the current position before calling `notifyDataSetChanged()`. – Reaz Murshed Jun 23 '17 at 22:32
  • That makes sense, `getFirstVisiblePosition()` will do it. I'll try that. Thank you! – ashwin mahajan Jun 23 '17 at 22:36
  • Glad to know that helped. – Reaz Murshed Jun 24 '17 at 05:57
0

If you are passing the list of PRODUCT objects via BaseAdapter constructor, You can get the position of the item whose cart is clicked by getChildAt(position). If the cart quantity is changed, based upon above position you can trace the item in ArrayList as well. So pass the new value at particular product at arraylist and call notifyDataSetChanged(). This will update your listview

If possible,I recommend to update ListView to use RecyclerView, It is more flexible than ListView.

Ait Bri
  • 370
  • 3
  • 13
  • Cart contains all the items, and cart is popped on another button click, not any product. I am looking at resetting the data and calling `notifyDataSetChanged`, which I want to avoid. – ashwin mahajan Jun 23 '17 at 22:22
  • Okay but how will recycler view help? – ashwin mahajan Jun 23 '17 at 22:29
  • 1
    ListView is deprecated and replaced with RecyclerView. The approach for both are somehow similar . The recyclerview adapter have function such as notifyItemChanged(position), which will check the particular item not the entire item – Ait Bri Jun 23 '17 at 22:42
  • I'll try that, Thanks. – ashwin mahajan Jun 23 '17 at 22:49