Android Question Linking Buttons to Edittext in CLV

Setlodi

Active Member
Hi there

Firstly I would like to thank Erel for B4X as well as all developers who are willing to help others.

I'm having a challenge which I hope you can assist with. I'm developing a POS app and I can't seem to get it to work as expected on the sales side. The app uses a dedicated PDA scanning device for scanning barcodes of products. After scanning a barcode, the app searches local SQLite database for products. When it finds it, it adds the product info into a custom list view. As users are scanning barcodes, products are added to he CLV. So far this works well.

Adding products to CLV:
        Dim p As Panel = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, Root.Width, 130dip)
        p.LoadLayout("Layout_Sales_View")
        lblProd_Name.Text = Product_Name
    
        Dim prod_list As List
        prod_list.Initialize
        prod_list.AddAll(Array As String(CLV_Product_Code, Sale_ID, CLV_Product_Name , CLV_Product_Price, CLV_Product_QTY, CLV_Line_Total))
        clvProduct_Sales.Add(p,prod_list)

In my layout I have a Product label, 2 buttons (Plus and Minus) plus an Edittext and a total label. Through the edittext, users are able to type in the quantity manually or they can use buttons to increase or decrease the quantity. In the total label, quantity and price are multiplied and displayed. So far so good.

Pressing Plus or Minus Buttons:
If IsNumber(txtEnterQTY.Text) Then
        Product_QTY = txtEnterQTY.Text
    Else
        Product_QTY = 0
    End If
    
    Product_QTY = Product_QTY + 1 ' Increase quantity by 1
    txtEnterQTY.Text = Product_QTY ' Update EditText
    Line_Total = Product_QTY*Product_Price
    lblLine_Total.Text =  f.Format(Line_Total)

My challenge is on manipulating the quantities of the items. After scanning the product, I press plus to increase the quantity or minus to decrease the quantity. After scanning the second product use the buttons likewise and it works. However, when I try to change the quantity of the first product by pressing the buttons in the CLV, it changes the edittext of the second product. I add more products and whenever I press buttons on the previous products, it always changes the edittext of the last item in the CLV.

Just checking the index etc...

Checking Index in Plus, Minus Buttons:
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    Dim p As B4XView = clvProduct_Sales.GetPanel(index)
    Dim pp As B4XView = p.GetView(0)
    
    Log("Button Plus index = " & index)
    Log("p = " & p)
    Log("pp = " & pp)

Logs...

B4X:
Button Plus index = 0
p = (BALayout): Left=0, Top=0, Width=720, Height=248
pp = (TextView): Left=19, Top=19, Width=519, Height=96, Tag=

My edittext only works on the last product in the CLV, irrespective of which button I press.

Please help. I'm stuck here...
 

aeric

Expert
Licensed User
Longtime User
Hi there

Here is my code at present:

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. 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 = 14
--- Grand_Total = 42
--- Grand_Total = 84

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=14.00}
Total_Map +++ = (MyMap) {0=28.00}
Total_Map +++ = (MyMap) {0=42.00}

After adding the second item and pressing the Plus button 2 times:
B4X:
Total_Map +++ = (MyMap) {0=42.00, 1=65.00}
Total_Map +++ = (MyMap) {0=42.00, 1=130.00}

So far so good, now I want to sum the values in the map, 42.00 and 130.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 = 84
--- Grand_Total = 149
--- Grand_Total = 279

The total should be 42.00 + 130.00 = 172.00

I'm stuck here and I need help please.
Thank you in advance.
I suggest you create a new thread for this question and upload a small example project.
 
Upvote 0
Top