Hi there
I'm having a problem summing up values from a map in a loop. The sum that I get is inconsistent.
Here is my code:
This doesn't seem to work because it still gives inconsistent totals. I searched product 1001 and press the search button. Whenever I press the Plus button, it doubles the total. Here is the log after I pressed the plus button 3 times:
Now what I did was put the values in a map
and the log after pressing Plus button 3 times:
After adding the second item (1002) and pressing the Plus button 2 times:
So far so good, now I want to sum the values in the map, 3.00 and 22.00 and I get a wrong total
When I run this code:
I get:
I'm stuck here and I need help please. Sample project attached.
Thank you in advance.
I'm having a problem summing up values from a map in a loop. The sum that I get is inconsistent.
Here is my code:
btnLblPlus_Click:
Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
Dim p As Panel = clvProduct_Sales.GetPanel(index)
Dim price As Label = p.GetView(1)
Dim qty As EditText = p.GetView(5)
Dim ttl As Label = p.GetView(2)
If IsNumber(qty.Text) Then
Product_QTY = qty.Text
Else
Product_QTY = 0
End If
Product_QTY = Product_QTY + 1 ' Increase quantity by 1
qty.Text = Product_QTY ' Update EditText
Line_Total = Product_QTY * price.Text
ttl.Text = f.Format(Line_Total)
Grand_Total = Grand_Total + ttl.text
Log("--- Grand_Total = " & Grand_Total)
This doesn't seem to work because it still gives inconsistent totals. I searched product 1001 and press the search button. Whenever I press the Plus button, it doubles the total. Here is the log after I pressed the plus button 3 times:
B4X:
--- Grand_Total = 1
--- Grand_Total = 3
--- Grand_Total = 6
Now what I did was put the values in a map
B4X:
Total_Map.Put(index, ttl.text)
Log("Total_Map +++ = " & Total_Map)
and the log after pressing Plus button 3 times:
B4X:
Total_Map +++ = (MyMap) {0=1.00}
Total_Map +++ = (MyMap) {0=2.00}
Total_Map +++ = (MyMap) {0=3.00}
After adding the second item (1002) and pressing the Plus button 2 times:
B4X:
Total_Map +++ = (MyMap) {0=3.00, 1=11.00}
Total_Map +++ = (MyMap) {0=3.00, 1=22.00}
So far so good, now I want to sum the values in the map, 3.00 and 22.00 and I get a wrong total
When I run this code:
B4X:
Log("Total_Map = " & Total_Map)
For Each LineTotal As String In Total_Map.Values
Log("** LineTotal = " & LineTotal )
Grand_Total = Grand_Total + LineTotal
Log("Grand_Total = " & Grand_Total)
Next
I get:
B4X:
--- Grand_Total = 42
--- Grand_Total = 64
The total should be 3.00 + 22.00 = 25.00
I'm stuck here and I need help please. Sample project attached.
Thank you in advance.