Android Question Sum Values from Map not Accurate

Setlodi

Active Member
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:

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.
 

Attachments

  • POS_Demo-2.zip
    117.9 KB · Views: 59
Solution
B4X:
Public Grand_Total As String = 0
You should not do calculation with String.

Change it to Double.
B4X:
Public Grand_Total As Double

aeric

Expert
Licensed User
Longtime User
B4X:
Public Grand_Total As String = 0
You should not do calculation with String.

Change it to Double.
B4X:
Public Grand_Total As Double
 
Upvote 0
Solution

Setlodi

Active Member
Thank you Daestrum and aeric. It works!

B4X:
Public Grand_Total As Double = 0
LineTotal As Double

B4X:
        Grand_Total = 0
        Log("Total_Map = " & Total_Map)
        For Each LineTotal As Double In Total_Map.Values
        Grand_Total = Grand_Total + LineTotal
        Log("Grand_Total = " & Grand_Total)
        txtSubtotal.Text =  f.Format(Grand_Total)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
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)
Obviously, there is a problem with the code snippet.
1st:
Product_QTY = Product_QTY+1=1 price.Text=1
ttl.text=1
Grand_Total = Grand_Total + ttl.text = 1
2nd:
Product_QTY = Product_QTY+1=2 price.Text=1
ttl.text=2
Grand_Total = Grand_Total + ttl.text = 1+2=3
3rd:
Product_QTY = Product_QTY+1=3 price.Text=1
ttl.text=3
Grand_Total = Grand_Total + ttl.text = 3+3=6
...
The correct Grand_Total should be Product_QTY*price(1) need not sum
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Check the example of B-POS.
It is better to pass a Custom Type or Map to the CustomListView as value.

B4X:
Type Product (Code As String, Name As String, Cost As Double, Price As Double, Qty As Int, Total As Double)
B4X:
Public Sub AddItemtoCLV (Item As Product)
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Root.Width, 80dip)
    p.LoadLayout("Layout_Sales_View")
    lblProd_Name.Text = Item.Name
    lblPrice.Text = f.Format(Item.Price)
    lblLine_Total.Text = f.Format(0)
    Sale_ID = Date&Time
    clvProduct_Sales.Add(p, Item)
    'ReCalculateGrandTotal
End Sub
B4X:
Private Sub btnLblPlus_Click   
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    UpdateLineItemQty(index, False, 1)
End Sub

Private Sub btnLblMinus_Click
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    UpdateLineItemQty(index, False, -1)
End Sub

Private Sub txtEnterQTY_EnterPressed
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    UpdateLineItemQty(index, True, 0) ' value not important
End Sub
B4X:
Private Sub UpdateLineItemQty (Index As Int, Exact As Boolean, Value As Int)
    Dim p As Panel = clvProduct_Sales.GetPanel(Index)
    Dim qty As EditText = p.GetView(5)
    Dim ttl As Label = p.GetView(2)
    Dim Item As Product = clvProduct_Sales.GetValue(Index)

    If Exact Then
        If IsNumber(qty.Text) Then
            Item.Qty = qty.Text
        Else
            Item.Qty = 0
        End If
    Else
        If Value = -1 Then
            If Item.Qty > 0 Then
                Item.Qty = Item.Qty - 1
            End If
        Else If Value = 1 Then
            Item.Qty = Item.Qty + 1
        End If
    End If
    
    qty.Text = Item.Qty ' Update EditText
    Item.Total = Item.Qty * Item.Price
    ttl.Text = f.Format(Item.Total)
    
    ReCalculateGrandTotal
    txtSubtotal.Text = f.Format(Grand_Total)
End Sub
B4X:
Private Sub ReCalculateGrandTotal
    Grand_Total = 0
    For i = 0 To clvProduct_Sales.Size - 1
        Dim LineItem As Product = clvProduct_Sales.GetValue(i)
        Log("** LineTotal = " & LineItem.Total)
        Grand_Total = Grand_Total + LineItem.Total
    Next
    Log("Grand_Total = " & f.Format(Grand_Total))
End Sub
 

Attachments

  • POS_Demo.zip
    117.9 KB · Views: 64
Upvote 0
Top