Android Question Buttons in xCustomListView

Hello there, I've a problem here. Look I have to work with buttons in my xCustomListView, I have a list with products where there are two buttons, one for add products to the cart and the other for remove ones, but when I touch one button in a random item does not change the specific product, only change the last item. How can I solve it?

2.JPG


For instance, in this case I touch the button which is indicated and only change the last item. My code is it:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("General")
    
    ListaProductos.Initialize
    ListaProductos.Add(CreateMap("nombre":"Harina COLOLO 0000 X 1kg", "marca":"COLOLO",  "comercio":"El Vasco",  "precio":90, "unidad":1, "imagen": LoadBitmap(File.DirAssets , "harina cololo.jpg")))
    ListaProductos.Add(CreateMap("nombre":"Alfajor Clásico PORTEZUELO chocolate", "marca":"PORTEZUELO",  "comercio":"El Vasco",  "precio":15, "unidad":1, "imagen": LoadBitmap(File.DirAssets , "alfajor.jpg")))
    ListaProductos.Add(CreateMap("nombre":"Aceite OPTIMO de girasol X 1lt ", "marca":"OPTIMO",  "comercio":"El Vasco",  "precio":45, "unidad":1, "imagen": LoadBitmap(File.DirAssets , "aceite.jpg")))
    ListaProductos.Add(CreateMap("nombre":"Arroz blanco SAMAN X 1kg", "marca":"SAMAN",  "comercio":"El Vasco",  "precio":50, "unidad":1, "imagen": LoadBitmap(File.DirAssets , "arroz.jpg")))
    
    Dim xui As XUI
    For Each newArt As Map In ListaProductos
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0,0, clv.AsView.Width, 190dip)
        p.LoadLayout("Producto_Super1")  'layout has 5 labels
        nombre.Text = newArt.Get("nombre")
        marca.Text ="Marca: " & newArt.Get("marca")
        comercio.Text = "Comercio: " & newArt.Get("comercio")
        precio.Text = "$" & newArt.Get("precio")
        unidad.Text = newArt.Get("unidad") & " un."
        imagen.Bitmap = newArt.Get("imagen")
        clv.Add(p, newArt)
    Next
    
    
End Sub

Sub agregar_Click
    
    For Each newArt As Map In ListaProductos
        Dim un As Int = newArt.Get("unidad")
        un = un + 1
        unidad.Text = newArt.Put("unidad", un) & " un."
    Next
    
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I'm a quite beginner but I'm going to try be more independent.
If you figured it out, that is great. If not, Export your project as zip and attach to a post. Someone will figure it out. In the meantime, here is a little code that can help give you some idea:
B4X:
Private Sub agregar_Click
    Dim Index As Int =clv1.GetItemFromView(Sender)
    Dim pnl As B4XView = clv1.GetPanel(Index)
    Log( "unidad: " & pnl.GetView(3).Text)   'Assuming the label that holds unidad is 4th in the designer views tree in the producto layout
    pnl.GetView(3).Text = pnl.GetView(3).Text +1  'Assuming the label that holds unidad is 4th in the designer views tree
    
    Dim m As Map= ListaProductos.Get(Index)
    m.Put("unidad", pnl.GetView(3).Text)
    Log(m.Get("unidad"))
End Sub
 
Upvote 0
Hello there! I have a question, I want to touch a button with the cart and add the item to a different module where there is a list with the items in the cart. I make this code but when y touch on the button it creates a new item in the active list.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("General")
    
    ListaProductos.Initialize
    ListaProductos.Add(CreateMap("nombre":"Harina COLOLO 0000 X 1kg", "marca":"COLOLO",  "comercio":"El Vasco",  "precio":90.0, "unidad":1.0, "imagen": LoadBitmap(File.DirAssets , "harina cololo.jpg")))
    ListaProductos.Add(CreateMap("nombre":"Alfajor Clásico PORTEZUELO chocolate", "marca":"PORTEZUELO",  "comercio":"El Vasco",  "precio":15.0, "unidad":1.0, "imagen": LoadBitmap(File.DirAssets , "alfajor.jpg")))
    ListaProductos.Add(CreateMap("nombre":"Aceite OPTIMO de girasol X 1lt ", "marca":"OPTIMO",  "comercio":"El Vasco",  "precio":45.0, "unidad":1.0, "imagen": LoadBitmap(File.DirAssets , "aceite.jpg")))
    ListaProductos.Add(CreateMap("nombre":"Arroz blanco SAMAN X 1kg", "marca":"SAMAN",  "comercio":"El Vasco",  "precio":50.0, "unidad":1.0, "imagen": LoadBitmap(File.DirAssets , "arroz.jpg")))
    
    Dim xui As XUI
    For Each newArt As Map In ListaProductos
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0,0, clv.AsView.Width, 190dip)
        p.LoadLayout("Producto_Super1")  'layout has 5 labels
        nombre.Text = newArt.Get("nombre")
        marca.Text ="Marca: " & newArt.Get("marca")
        comercio.Text = "Comercio: " & newArt.Get("comercio")
        unidad.Text = newArt.Get("unidad")
        precio.Text = newArt.Get("precio")
        imagen.Bitmap = newArt.Get("imagen")
        clv.Add(p, newArt)
    Next

End Sub

Sub carrito_Click
    
    Dim xui As XUI
    Dim p As B4XView = xui.CreatePanel("")
    
        p.SetLayoutAnimated(0, 0,0, clv.AsView.Width, 190dip)
        p.LoadLayout("Producto_Super1")
        nombre.Text = newArt.Get("nombre")
        marca.Text ="Marca: " & newArt.Get("marca")
        comercio.Text = "Comercio: " & newArt.Get("comercio")
        unidad.Text = newArt.Get("unidad")
        precio.Text = newArt.Get("precio")
        imagen.Bitmap = newArt.Get("imagen")
        clv.Add(p, newArt)
    
End Sub

WhatsApp Image 2021-01-11 at 23.31.48.jpeg
Like that
 
Upvote 0
Top