Android Question Problem with JRDC2 and CustomListView

cristian petersen

Member
Licensed User
Longtime User
I am having a problem that I am unable to understand.
What I am trying to accomplish is to show data in a vertical customlistview inside of it where are some labels and an horizontal customlistview.
This horizontal customlistview contains pictures.
The format is similar to Instagram, the idea is to show a vertical list of products with data and horizontal list of pictures.
Customlistview are filled with external data using JRDC2.
The problem occurs when I try to fill the horizontal customlistview with pictures. The pictures are not loading in the horizontal
customlistview corresponding to the product. They are loading in the last horizontal customlistview.
wrong.jpg


Things are better if I use a sleep between retrieving data between products. But I am sure that is not right.
right.jpg


I hope these products inspire someone to help me :)

Thanks in advance

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#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_productos As CustomListView
    Private iv_foto As B4XView
    Private lb_precio As B4XView
    Private xui As XUI
    Private Lista_fotos_producto As CustomListView
    Private et_cantidad As B4XView
    Private lb_producto_nombre As B4XView
    Private lb_iva As B4XView
    Private lb_categoria As B4XView
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("Lista_productos")
    lb_categoria.Text = Main.g_categoria
    Busca_Productos(19) 'Hardcoded to ilustrate
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Busca_Productos(p_categoria As Int)
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "select_productos_categoria"
    cmd.Parameters = Array As Object(p_categoria)
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand(cmd.Name, cmd.Parameters )
    Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(hj As HttpJob)
    If hj.Success Then
        req.HandleJobAsync(hj, "req")
        Wait For (req) req_Result(resultado As DBResult)
        carga_listView_productos(resultado)
    Else
        MsgboxAsync("Ha habido un error con la conexion","Error")
        Log("ERROR: " & hj.ErrorMessage)
    End If
    hj.Release
End Sub

Sub carga_listView_productos(resultado As DBResult)
    For i = 0 To resultado.Rows.Size - 1
        Dim fila() As Object = resultado.Rows.Get(i)
        Log("carga_listView_productos -  (resultado.Columns.getValueAt(0))"&fila(resultado.Columns.getValueAt(0))&" "&fila(resultado.Columns.getValueAt(0)))
        CLV_productos.Add(CreateListItem(fila, CLV_productos.AsView.Width, 200dip),"1")
        'Sleep(450)   <= I AM PRETTY SURE THIS IS WORNG
    Next
End Sub

Sub CreateListItem(fila As List, Width As Int, Height As Int) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("producto")
    lb_producto_nombre.tag = fila.get(0)
    lb_producto_nombre.text = fila.get(1)
    lb_precio.text = fila.get(2)
    lb_iva.text        =fila.get(3)
    busca_fotos_producto(fila.get(0))
    Return p
End Sub

Sub busca_fotos_producto(p_producto As Int)
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "select_productos_fotos"
    cmd.Parameters = Array As Object(p_producto)
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand(cmd.Name, cmd.Parameters )
    
    Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
    If j.Success Then
        req.HandleJobAsync(j, "req")
        Wait For (req) req_Result(resultado As DBResult)
        carga_listView_fotos_producto(resultado)
    Else
        MsgboxAsync("Ha habido un error con la conexion","Error")
        Log("ERROR: " & j.ErrorMessage)
    End If
    j.Release
End Sub


Sub carga_listView_fotos_producto(resultado As DBResult)
    For i = 0 To resultado.Rows.Size - 1
        Dim fila() As Object = resultado.Rows.Get(i)
        Lista_fotos_producto.Add(CreateListItemFotos(fila, Lista_fotos_producto.AsView.Width, 200dip),"1")
    Next
End Sub

Sub CreateListItemFotos(fila As List, Width As Int, Height As Int) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("foto_producto")

    Dim oBitMap As Bitmap
    Dim Buffer() As Byte
    Buffer = fila.Get(1) ' Image Column
    If Buffer <> Null Then
        Dim req As DBRequestManager = CreateRequest
        oBitMap = req.BytesToImage(Buffer)
        iv_foto.SetBitmap ( oBitMap)
    End If
    p.Height = iv_foto.Height
    p.Width = iv_foto.Width
    Return p
End Sub

Sub CreateRequest As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, Main.rdcLink)
    Return req
End Sub

Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = Name
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub
 
Top