Skip to content
Advertisement

sum value from any list item in Recyclerview Adapter

This is my CartAdapter code, and in onBindViewHolder it get data from Getter and Setter (Get Cart). My purpose is to sum all of the product prices from all data that shows up in cart fragment.

JavaScript

And this is my Log result from get total price :

JavaScript

and not even get total price, but just get each price separatedly

thankyou Nigel Brown, I’ve tried your answer and got this on my Log:

JavaScript

this is my fragment that provides all item has been added to the cart list :

This is my Screenshoot from Cart List Fragment

Advertisement

Answer

the problem is you declared the tsum in the for loop so it is getting set to zero each time it should be:

JavaScript

edit:

Based on your update from your question you should not be trying to make the total within the adapter. The onBindViewHolder will be called over and over as the views are created. You need to generate the views with your list and then once the list is made add up, use the totals from the items in the list and display in the view.

You will need to save each quantity for the items in getCart and then outside the adapter use the loop to go though each item and multiply the quantity by the price and add it to the total, do it for all items and you should be good.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement