Android Question Linking Buttons to Edittext in CLV

Setlodi

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...
 

Setlodi

Member
Are you declaring this in class scope and use it directly?
I think you should cast it and read its value inside the event sub.

Maybe something like this:
B4X:
Dim pp As EditText = p.GetView(0)
Log(pp. Text)
Thanks for your reply aeric, but I'm not sure I understand what you mean by "cast it and read its value inside the event sub". Please explain further...

Thank you
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks for your reply aeric, but I'm not sure I understand what you mean by "cast it and read its value inside the event sub". Please explain further...

Thank you
Event sub is like sub button_click. Since you didn't show it I can't tell.

Cast is just use equal sign. From right is an object and the left side is the target type.
 
Upvote 0

Setlodi

Member
Thank you so much aeric. I was able to determine the following:
  1. Quantity EditText = p.GetView(4)
  2. Line total Label = p.GetView(6)
B4X:
Private Sub btnPlus_Click
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    Dim p As Panel = clvProduct_Sales.GetPanel(index)
    Dim txtEnterQTY As EditText = p.GetView(4)
    Dim lblLine_Total As Label = p.GetView(6)

    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)
End Sub

It now works as expected.

Thank you once more aeric!!
 
Upvote 0

Setlodi

Member
Actually, after thorough testing, I realized that it's not really working as it should. Tis is what's happening:
  • I scan a product and it is added on the CLV
  • I press + or - to change the quantity
  • The line total is updated accordingly
  • I click the second product and it is loaded on the CLV
  • I press + or - to change the quantity
  • The line total is updated accordingly
  • When I press + or - on the first product, it takes line total from product 2 and puts it in product 1
What could be wrong?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Actually, after thorough testing, I realized that it's not really working as it should. Tis is what's happening:
  • I scan a product and it is added on the CLV
  • I press + or - to change the quantity
  • The line total is updated accordingly
  • I click the second product and it is loaded on the CLV
  • I press + or - to change the quantity
  • The line total is updated accordingly
  • When I press + or - on the first product, it takes line total from product 2 and puts it in product 1
What could be wrong?
Please upload a small example.
 
Upvote 0

Setlodi

Member
Please upload a small example.
Hi aeric

Thank you for your response. Attached please find a stripped version of my code. From the main page press Sales and enter the product code in the edittext. Click search and the product details will be loaded on the CLV. You can enter the quantity by typing it in or pressing + or - buttons.

The product codes to use are 1001, 1002, 1003, 1004, 1005

Do not mind any other buttons as I use them for debugging.

The amount on the right of the screen is the line total which is supposed to be price (amount on the left) multiplied by quantity. Please watch what happens when you press + or - on the first product after you have loaded a few products. It shows the amount on the right.

Thank you in advance.
 

Attachments

  • POS_Demo.zip
    119.3 KB · Views: 28
Upvote 0

aeric

Expert
Licensed User
Longtime User
You have mixed up the variables.
As I have answered earlier, you need to redeclare the views.
You cannot use global variables. You need to use new names.
And your price label has currency symbol in front.

B4X:
Private Sub btnPlus_Click

    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    Dim p As Panel = clvProduct_Sales.GetPanel(index)
    
    Log("0 = " & p.GetView(0))
    Log("1 = " & p.GetView(1))
    Log("2 = " & p.GetView(2))
    Log("3 = " & p.GetView(3))
    Log("4 = " & p.GetView(4))
    Log("5 = " & p.GetView(5))
        
    Dim price As Label = p.GetView(1)
    Dim qty As EditText = p.GetView(3)
    Dim ttl As Label = p.GetView(5)
    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.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
End Sub
 
Upvote 0

Setlodi

Member
You have mixed up the variables.
As I have answered earlier, you need to redeclare the views.
You cannot use global variables. You need to use new names.
And your price label has currency symbol in front.

B4X:
Private Sub btnPlus_Click

    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    Dim p As Panel = clvProduct_Sales.GetPanel(index)
   
    Log("0 = " & p.GetView(0))
    Log("1 = " & p.GetView(1))
    Log("2 = " & p.GetView(2))
    Log("3 = " & p.GetView(3))
    Log("4 = " & p.GetView(4))
    Log("5 = " & p.GetView(5))
       
    Dim price As Label = p.GetView(1)
    Dim qty As EditText = p.GetView(3)
    Dim ttl As Label = p.GetView(5)
    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.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
End Sub
aeric, thank you once more for your response.

Upon testing, I have discovered something else. If I have more than 1 product in my CLV, whenever I press - on the previous items (not last one), it writes quantity of last item minus 1.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
aeric, thank you once more for your response.

Upon testing, I have discovered something else. If I have more than 1 product in my CLV, whenever I press - on the previous items (not last one), it writes quantity of last item minus 1.
Have you modify the code in btnMinus?
You need to apply the same concept.
 
Upvote 0

Setlodi

Member
Yes I have modified it. I even made different variables from the Plus button but still the same.

Plus and Minus Buttons:
Private Sub btnPlus_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(3)
    Dim ttl As Label = p.GetView(5)
    
    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.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
    
End Sub

Private Sub btnMinus_Click
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    Dim p As Panel = clvProduct_Sales.GetPanel(index)

    Dim price2 As Label = p.GetView(1)
    Dim qty2 As EditText = p.GetView(3)
    Dim ttl2 As Label = p.GetView(5)
    
    
    If IsNumber(txtEnterQTY.Text) Then
        Product_QTY = txtEnterQTY.Text
    Else
        Product_QTY = 0
    End If
    If Product_QTY > 0 Then
        Product_QTY = Product_QTY - 1 ' Decrease quantity by 1
    End If
    
    qty2.Text = Product_QTY ' Update EditText
    Line_Total = Product_QTY * price2.Text.As(String).SubString(2).As(Double)
    ttl2.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yes I have modified it. I even made different variables from the Plus button but still the same.

Plus and Minus Buttons:
Private Sub btnPlus_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(3)
    Dim ttl As Label = p.GetView(5)
   
    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.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
   
End Sub

Private Sub btnMinus_Click
    Dim index As Int = clvProduct_Sales.GetItemFromView(Sender)
    Dim p As Panel = clvProduct_Sales.GetPanel(index)

    Dim price2 As Label = p.GetView(1)
    Dim qty2 As EditText = p.GetView(3)
    Dim ttl2 As Label = p.GetView(5)
   
   
    If IsNumber(txtEnterQTY.Text) Then
        Product_QTY = txtEnterQTY.Text
    Else
        Product_QTY = 0
    End If
    If Product_QTY > 0 Then
        Product_QTY = Product_QTY - 1 ' Decrease quantity by 1
    End If
   
    qty2.Text = Product_QTY ' Update EditText
    Line_Total = Product_QTY * price2.Text.As(String).SubString(2).As(Double)
    ttl2.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
End Sub
compare with my example.
You haven't completely modify the code.
 
Upvote 0

Setlodi

Member
compare with my example.
You haven't completely modify the code.
Yep, I saw my mistake on the Minus button. You are a star aeric!

For all those who might benefit from this post, my mistake was I didn't change txtEnterQTY to qty as per aeric example.

The updated Plus and Minus buttons:

Plus and Minus buttons:
Private Sub btnPlus_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(3)
    Dim ttl As Label = p.GetView(5)
   
    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.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
   
End Sub

Private Sub btnMinus_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(3)
    Dim ttl As Label = p.GetView(5)
   
    If IsNumber(qty.Text) Then
    Product_QTY = qty.Text
        Else
    Product_QTY = 0
        End If
    If Product_QTY > 0 Then
    Product_QTY = Product_QTY - 1 ' Decrease quantity by 1
        End If
   
    qty.Text = Product_QTY ' Update EditText
    Line_Total = Product_QTY * price.Text.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
   
End Sub

Thank you so much for your patience and willingness to help!!!!
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yep, I saw my mistake on the Minus button. You are a star aeric!

For all those who might benefit from this post, my mistake was I didn't change txtEnterQTY to qty as per aeric example.

The updated Plus and Minus buttons:

Plus and Minus buttons:
Private Sub btnPlus_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(3)
    Dim ttl As Label = p.GetView(5)
  
    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.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
  
End Sub

Private Sub btnMinus_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(3)
    Dim ttl As Label = p.GetView(5)
  
    If IsNumber(qty.Text) Then
    Product_QTY = qty.Text
        Else
    Product_QTY = 0
        End If
    If Product_QTY > 0 Then
    Product_QTY = Product_QTY - 1 ' Decrease quantity by 1
        End If
  
    qty.Text = Product_QTY ' Update EditText
    Line_Total = Product_QTY * price.Text.As(String).SubString(2).As(Double)
    ttl.Text =  f.Format(Line_Total)
    lblSubtotal.Text =  f.Format(Sale_Total)
  
End Sub

Thank you so much for your patience and willingness to help!!!!
Good that now you have understood.
You can use other naming which is more meaningful.

Just don't use the name inside Layout Designer. You will be confused.

It is like the customlistview contains many same views with the same name and how to know which one you are referring to.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
When you load the layout set the tag property of the buttons to the edit text. Then in the button_click sub get the correct button, get the tag property and set the value.
This is B4j But something like this:
B4X:
Private Sub PlusBtn_Click
    Dim Btn As Button = Sender
    Dim Txt As TextField = Btn.Tag
    Txt.Text = Txt.Text + 1
..
..
 
Upvote 0
Top