Skip to content
Advertisement

Sum column SQLite Android and display on TextView

I have an SQLite DB in my Android Project. I have a RecyclerView for “Cart” Activity, where I display some items from this DB. Every item has its own “quantity” and “price”. What I want to do, is to create a method to display a total sum of elements (Total price = Elem1(quantity * price) + Elem2(quantity * price)+…) in one TextView. I let down my database. I appreciate any help!

JavaScript

Advertisement

Answer

Change the method sumPriceCartItems() of your DatabaseHelper to this:

JavaScript

so by using only 1 Cursor and without the need of a loop it returns the sum that you want.
In your activity’s code get the sum and display it in the TextView:

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