Skip to content
Advertisement

Android Studio calculate total price

I wanna ask how can I sum all patty price and display in the total ? The total is in activity_main.xml layout while all the product price is in view_product.xml . Screenshot of app As you can see the grey color text is per patty price , and the right side black text is price after multiply the quantity of patty. I want all the black text to sum up and show at below total.

MainActivity.java

JavaScript

ProductsAdapter.java

JavaScript

Product.java

JavaScript

activity_main.xml

JavaScript

view_product.xml

JavaScript

Advertisement

Answer

In view_product.xml you are only displaying the price, but that’s not where the actual data is located so your question shouldn’t be that the prices are in view_product.xml and you want to show in activity_main.xml, but your question should be –

  1. Where is the data located
  2. How to make the necessary calculations
  3. Where to show it. .

Your data regarding the prices are in products object. So, in order to calculate the total you just need to add one more method which calculates the total

JavaScript

The result of this you can store in a new value or show directly in the textView itself.

JavaScript

This you can call anywhere after createInitialProductList(). I hope this helps and makes it clear for you.

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