Android Question [SOLVED]How to Sum the items of a listView (Not count them)

Daniel44

Active Member
Licensed User
Hi everyone

I'm working with a listview (I know that I must not be working with a lv but a clv but I guess If I work wit a CLv I'd have the same issues:))

I fill it by adding addTwoline with spinners value (spName and spPrice)

B4X:
LV1.AddTwoLines(spName.SelectedItem,SpPrice.SelectedItem)

and it's woriking fine...but what I can't do is to sum all that spPrice values in the Listview to get a Total of all that items

I've been working by adding all items to a list in order sum all later but I can't get (I don't know how to do it) each spPrice item

I just get the spName values not spPrice values

I've done a Sub that sum all that item but that executes itself on the spPrice_ItemClick and working fine but I know I need a "function" out that event because when I remove items I need that sum items left.


B4X:
lbltotal.Text = "$: 0,00"
    value1 = SpPrice.SelectedItem
    value2 = value2 + value1
    Log(value2)
    lbltotal.Visible = True
    Total = value2
    lbltotal.Text = "Monto Total $: "&Total

I'll appreciate it if someone can help me with this. Thank you in advance
 

drgottjr

Well-Known Member
Licensed User
Longtime User
i think i follow what you are describing.

by default, the first line of a 2-line list view item is returned when you click on that item.
this is what you are experiencing. You could reverse lines 1 and 2 and show the price first,
but that might look strange.

There is another option: have you tried LV1.AddTwoLines2( spName.SelectedItem,SpPrice.SelectedItem, SpPrice.Price???)
what happens in this case is when you click on the item, that 3rd parameter is returned. i don't know
exactly what SpPrice is (a custom type?). i would image you keep its price somewhere. that price
is what you want to put as the 3rd parameter to LV1.AddTwoLines2(). is that understood?

if you show lbltotal on every item click, it will show a running sum of prices. You could also keep
a global variable with a running sum of prices.
 
Upvote 0

Daniel44

Active Member
Licensed User
Hey @drgottjr thank you for asking

Well you're saying SpPrice.Price.. but spPrice is a spinner it doesn't have that method. I have a button where I'll put a function that sum all without clicking anywhere in the listview.

spPrice.SelectedItem is a double value. and that is added to the listview (when I click on the spName). Once I added all product I wish I should press the button to execute a "function" that sum all items added in the listview I hope it can be understood what I need my english is too bad.

Thank you
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The fact that you are populating a listview implies that you have a source of the items names and prices, perhaps a SQLite database table, a text file, a list, a map, etc. Why can't you sum them before you add them to the listview. If that is doable in your situation, that is the easier way to do. Even if you are selecting a certain number of items that meet a certain criteria and filter, you can apply it to the source.
 
Upvote 0

Daniel44

Active Member
Licensed User
The fact that you are populating a listview implies that you have a source of the items names and prices, perhaps a SQLite database table, a text file, a list, a map, etc. Why can't you sum them before you add them to the listview. If that is doable in your situation, that is the easier way to do. Even if you are selecting a certain number of items that meet a certain criteria and filter, you can apply it to the source.
Hey Mahares my source is sqlite and first I fill the spPrice with a basic function then the Lv is filled by clicking that spinner. The spPrice click event has a method that sum in each click and that is what I don't want. I just want fill the Lv then with another function outside that click event (for example a button click) to sum each item in the LV.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is really a waste of time to use and discuss ListView.

You should add the price as the value and calculate it with:
B4X:
CLV.Add(CreateItem(Name, Price), Price)

Sub Sum as Double
 Dim Total As Double
For i = 0 To CLV.Size - 1
        Total = Total + CLV.GetValue(i)
    Next
Return Total
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You should add the price as the value and calculate it with:
Why can't our Argentinian friend simply use a single line query execution to get the result he wanted; a WHERE clause can also be added if need be.
B4X:
Dim total As Double =Starter.SQL1.ExecQuerySingleResult("SELECT SUM(IFNULL(price,0)) FROM mytable")
Log(total)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
ListView / CLV are UI elements. The role of UI elements is to show something to the user. It is always a better design not to confuse the data store with the visual representation. This provides much more flexibility.

With that said, getting the data from the list is also simple.
 
Upvote 0

Daniel44

Active Member
Licensed User
It is really a waste of time to use and discuss ListView.

You should add the price as the value and calculate it with:
B4X:
CLV.Add(CreateItem(Name, Price), Price)

Sub Sum as Double
Dim Total As Double
For i = 0 To CLV.Size - 1
        Total = Total + CLV.GetValue(i)
    Next
Return Total
End Sub
Thank you Erel I will try that.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
As drgottjr wrote, you can use the addTwoLines2 method:
B4X:
lv1.AddTwoLines2(spname.SelectedItem,spprice.SelectedItem,spprice.SelectedItem)

Your sum function can be as follows:

B4X:
Sub SumValues
   
   Private Total As Double 
   
   For i=0 To lv1.Size-1
        Total=Total+lv1.GetItem(i)
   Next
   
   lbltotal.Visible = True
   lbltotal.Text = "Monto Total $: "&Total

End Sub


 
Upvote 0

Daniel44

Active Member
Licensed User
As drgottjr wrote, you can use the addTwoLines2 method:
B4X:
lv1.AddTwoLines2(spname.SelectedItem,spprice.SelectedItem,spprice.SelectedItem)

Your sum function can be as follows:

B4X:
Sub SumValues
   
   Private Total As Double 
   
   For i=0 To lv1.Size-1
        Total=Total+lv1.GetItem(i)
   Next
   
   lbltotal.Visible = True
   lbltotal.Text = "Monto Total $: "&Total

End Sub

Hi @drgottjr thank you so much!! it is working fine!!! Thanks a lot!
 
Upvote 0
Top