Android Question How can I use maps values in a xCustomList View?

Hi guys, how are you? Hope fine. I want to make a supermarket product list with a xCustomList View like that





I'm going to add 10 products with 10 different maps but I don't know how to use these maps values in the different items (i made an item view with the different places where information should be), could you help me? I mean, I want to write ten different maps like this

B4X:
Dim newArt(5) as Map

    newArt(0).Initialize
    newArt(0).Put("nombre","Arroz blanco SAMAN 1kg.")
    newArt(0).Put("marca","SAMAN")
    newArt(0).Put("comercio","'El Vasco'")
    newArt(0).Put("precio", 50)
    newArt(0).Put("unidad" , 1)

And put this information in the different labels.

Here is my complete code
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private clv As CustomListView
    
    Private comercio As B4XView
    Private imagen As B4XView
    Private marca As B4XView
    Private nombre As B4XView
    Private precio As B4XView
    Private unidad As B4XView
    
    Dim newArt (5) As Map

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("General")
    
    newArt(0).Initialize
    newArt(0).Put("nombre","Arroz blanco SAMAN 1kg.")
    newArt(0).Put("marca","SAMAN")
    newArt(0).Put("comercio","'El Vasco'")
    newArt(0).Put("precio", 50)
    newArt(0).Put("unidad" , 1)
    
    newArt(1).Initialize
    newArt(1).Put("nombre","Harina de trigo COLOLO 0000 1kg.")
    newArt(1).Put("marca","COLOLO")
    newArt(1).Put("comercio","'El Vasco'")
    newArt(1).Put("precio", 30)
    newArt(1).Put("unidad" , 1)

    
    Dim xui As XUI
    For i =0 To 5
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(100, 1%x, 1%x, 98%x, 195dip)
        p.LoadLayout("Producto_Super1")
        clv.Add(p, "")
        If i = 0 Then
            nombre.Text = newArt(0).Get("nombre")
            marca.Text ="Marca: " & newArt(0).Get("marca")
            comercio.Text = "Comercio: " & newArt(0).Get("comercio")
            precio.Text = "$" & newArt(0).Get("precio")
            unidad.Text = newArt(0).Get("unidad") & "un."

        End If
    Next 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

If you need more information please let me know. See you, Bruno
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't use array of maps. Use List of maps. More flexible.

B4X:
For Each m As Map in ListOfMaps
 '....
            nombre.Text = m.Get("nombre")
            marca.Text ="Marca: " & m.Get("marca")
            comercio.Text = "Comercio: " & m.Get("comercio")
            precio.Text = "$" & m.Get("precio")
            unidad.Text = m.Get("unidad") & "un."

Next
 
Upvote 0
It's the last item on the list, what I'm doing wrong?



B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private clv As CustomListView
    
    Private comercio As B4XView
    Private imagen As B4XView
    Private marca As B4XView
    Private nombre As B4XView
    Private precio As B4XView
    Private unidad As B4XView
    
    Dim ListOfMaps As List
    Dim m As Map

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("General")
    
    Dim xui As XUI
    For i =0 To 5
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(100, 1%x, 1%x, 98%x, 195dip)
        p.LoadLayout("Producto_Super1")
        clv.Add(p, "")
    Next
    
    m.Initialize
    ListOfMaps.Initialize
    ListOfMaps.Add(m)
    
    For Each m As Map In ListOfMaps
        
    m.Put("nombre","Arroz blanco SAMAN 1kg.")
    m.Put("marca","SAMAN")
    m.Put("comercio","'El Vasco'")
    m.Put("precio", 50)
    m.Put("unidad" , 1)
    
    m.Put("nombre","Harina de trigo COLOLO 0000 1kg.")
    m.Put("marca","COLOLO")
    m.Put("comercio","'El Vasco'")
    m.Put("precio", 30)
    m.Put("unidad" , 1)
    
        nombre.Text = m.Get("nombre")
        marca.Text ="Marca: " & m.Get("marca")
        comercio.Text = "Comercio: " & m.Get("comercio")
        precio.Text = "$" & m.Get("precio")
        unidad.Text = m.Get("unidad") & "un."
    Next
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User

Look this example : https://www.b4x.com/android/forum/threads/cards-list-with-customlistview.87720/#content
 
Upvote 0
What do you expect your code to do? You need to somehow fill the maps and add them to the ListOfMaps.

I need to create a list of products for a supermarket, I add products to the list with CRUD and it shows in the app by a xCustomListView. This xCustomListView have many items and each item has different information. So I have to create a generic map where can I add each product with his information and add him in a List. My question is, is it possible to create a unique map called "m" and add all the articles of the supermarket?

Lo voy a explicar en español, quizá sea mas claro. Tengo que crear una lista de productos de supermercado, cada producto en mi código es un item de xCustomListView, cada item tiene diferente información la cual se debe mostrar al público, por ejemplo:

item #1:
Nombre = Arroz
Marca = SAMAN
Comercio = El Campo
Precio = $50

item #2:
Nombre = Azucar
Marca = COLOLO
Comercio = El Campo
Precio = $90

Cada item es un map en una lista de productos. Me gustaria saber si creando un map generico llamado "m" puedo agregar esta información sin necesidad de crear otro map diferente, asi puedo crear un solo codigo para la etiqueta de cada producto de esta manera:

B4X:
        nombre.Text = m.Get("nombre")
        marca.Text ="Marca: " & m.Get("marca")
        comercio.Text = "Comercio: " & m.Get("comercio")
        precio.Text = "$" & m.Get("precio")
        unidad.Text = m.Get("unidad") & "un."
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
what I'm doing wrong?
Hi Bruno:
Your code needs to be something like this to get you started:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("General")  'layout has CustomListView: clv. You can also use B4Xpages project
 
    MyList.Initialize   
    MyList.Add(CreateMap("nombre":"Arroz blanco SAMAN 1kg", "marca":"SAMAN",  "comercio":"El Vasco",  "precio":50, "unidad":1))
'    You keep adding the maps to the list by creating them like above or by the way you were doing it. Note I only put one map

    For Each m As Map In MyList
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0,0, clv.AsView.Width, 85dip)
        p.LoadLayout("Producto_Super1")  'layout has 5 labels
        nombre.Text = m.Get("nombre")
        marca.Text = m.Get("marca")
        comercio.Text = m.Get("comercio")
        precio.Text = m.Get("precio")
        unidad.Text = m.Get("unidad")
        clv.Add(p, m)
    Next
End Sub
 
Upvote 0

Thank you very much Mahares, I am very happy to have been able to receive your help, I was a bit confused until you helped me, I repeat, thank you very much.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…