iOS Question B4i Download Images into a CustomListView

Discussion in 'iOS Questions' started by tamayo461, Feb 6, 2015.

  1. tamayo461

    tamayo461 Member Licensed User

    Please help, I am trying to insert images into a CustomListView, the images are downloaded from internet, the code works half brings me updated list but only the last image of CustomListView.(see img below)

    [​IMG]

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Private lst_tragos = "lst_tragos", fot_marca = "fot_marca", fot_pro = "fot_pro"
        
    Dim lv As CustomListView
        
    Type treslineas (First As String, Second As String, Tercero As String)
        
    Dim temp As Boolean
        
    Private pg As Page
        
    Private Label1 As Label
        
    Private ncarrito As Label
        
    Private carrito As ImageView
        
    Private foto_marca As ImageView
        
    Dim imgmarca As String
        
    'Dim b As Bitmap
        Dim im As ImageView
    End Sub

    Public Sub Show
        
    If pg.IsInitialized = False Then
            pg.Initialize(
    "pg")
            pg.RootPanel.LoadLayout(
    "carta")
        
    End If
        Main.NavControl.ShowPage(pg)
        im.Initialize(
    "")
        llenar_lista
    End Sub

    Sub cargar_img(fot As String)
        des_img(
    "http://www.xxxxxx.com/app/exxxxs/images/mxxx/" & fot, fot_marca)
    End Sub

    Sub llenar_lista
        ExecuteRemoteQuery(
    "select * from inventario where categoria = '"&Main.categoria&"' and marca = '"&Main.marca&"'", lst_tragos)
    End Sub

    Sub ExecuteRemoteQuery(Query As String, JobName As String)
        
    Dim job As HttpJob
        job.Initialize(JobName, Me)
        job.PostString(
    "http://www.xxxxx.com/xxxx_xxx.php", Query)
    End Sub

    Sub des_img(url As String, JobName As String)
        
    Dim job1 As HttpJob
        job1.Initialize(JobName, Me)
        job1.Download(url)
    End Sub

    Sub JobDone(Job As HttpJob)
        lv.Initialize(Me, 
    "lv"100%X)
        pg.RootPanel.AddView(lv.AsView, 
    00100%x150%y)
        lv.SetSize(Label1.Width, 
    150%y)
        lv.AsView.Left = 
    0%x
        lv.AsView.Top = 
    0%y + foto_marca.Top + foto_marca.Height + 3dip
        lv.AsView.Color = 
    Colors.ARGB(25,0,0,0)
        
    'ProgressDialogHide
        If Job.Success Then 
            
    'Log("Response from server: " & res) 
            Select Job.JobName
          
    '***********************************************
                Case lst_tragos
                    
    Dim res As String
                    res = Job.GetString
                    
    Dim parser As JSONParser
                    parser.Initialize(res)
                    
    Dim l2 As List
                    lv.Clear
                    l2 = parser.NextArray
                    
    If l2.Size = 0 Then
                        
    Msgbox("No se encontraron registros"False)
                    
    Else
                    
    Log (l2.Size)
                        
    For i = 0 To l2.Size - 1             
                            
    Dim tl As treslineas
                            
    Dim m As Map
                            m = l2.Get(i)
                            tl.First = m.Get(
    "nombre")
                           tl.Second = m.Get(
    "foto")
                            tl.Tercero = m.Get(
    "descripcion")& Chr(13) & Chr(10) & " Valor: $" & m.Get("valor")
                            imgmarca = m.Get(
    "foto_marca")         
                            lv.Add(CreateListItem(tl.First,tl.Second , 
    120%x70dip), 70dip"Item")              
                        
    Next
                        
    'Msgbox(imgmarca,"")
                        cargar_img(imgmarca)
                    
    End If
              
    '****************************************************
                Case fot_marca         
                    foto_marca.Bitmap=Job.GetBitmap
              
    '************************************************
                Case fot_pro     
                    im.Bitmap = Job.GetBitmap
                
    End Select 
        
    Else
            
    Msgbox("Error: " & Job.ErrorMessage, True)
        
    End If
        Job.Release
    End Sub

    Sub CreateListItem(Text As String,foto As String, Width As Int, Height As Int) As Panel
        
    Dim m2 As Map
        m2.Initialize
        
    Dim p As Panel
        p.Initialize(
    "")
        p.Color = 
    Colors.Black
        
    'Dim b As Bitmap
        'b.Initialize(File.DirAssets,"flecha.png")
        Dim im As ImageView
        im.Initialize(
    "")
        
    'im.Bitmap = b
        Dim lbl As Label
        lbl.Initialize(
    "lbl")
        lbl.TextAlignment = lbl.ALIGNMENT_LEFT
        lbl.Text = Text
        lbl.Font = 
    Font.CreateNew(13)
        lbl.TextColor = 
    Colors.White
        p.AddView(lbl, Height+
    5dip,0,Width*0.8,Height)
        des_img(
    "http://www.xxxxxx.com/axxxxp/exxxxxes/images/xxxx/" & foto, fot_pro)
        p.AddView(im, 
    0,0,Height,Height) 
        
    Return p
    End Sub
     
  2. jcesar

    jcesar Active Member Licensed User

    Is there more code ? I think something is missing,
     
  3. tamayo461

    tamayo461 Member Licensed User

    this is all the code
     
  4. jcesar

    jcesar Active Member Licensed User

  5. tamayo461

    tamayo461 Member Licensed User

    ok, I will analyze the code and if anything I commented as I was. Thanks for help.
     
  6. tamayo461

    tamayo461 Member Licensed User

  7. xulihang

    xulihang Active Member Licensed User

    I use imagedownloader to load images in b4a. But there is not an implementation of imagedownloader in b4i.
     
  8. tamayo461

    tamayo461 Member Licensed User

    @Erel any ideas? some example?
     
  9. tamayo461

    tamayo461 Member Licensed User

    @jcesar apparently has accommodated the code.
     
  10. walterf25

    walterf25 Well-Known Member Licensed User

    I don't see anywhere in your code where you're assigning the downloaded image to the im as imageview variable, i think that's your problem.

    Cheers,
    Walter
     
  11. tamayo461

    tamayo461 Member Licensed User

    This code section regarding downloading images works well; in fact the designated image is discharged with that method. The problem with the code is that all the images, put them in the last row.


    upload_2015-2-9_14-54-10.png
     
  12. tamayo461

    tamayo461 Member Licensed User


    look here :

    Case fot_pro
    im.Bitmap = Job.GetBitmap
     
  13. walterf25

    walterf25 Well-Known Member Licensed User

    It seems like an index problem to me, but is hard to tell without seeing the entire project.
     
  14. tamayo461

    tamayo461 Member Licensed User

    Thanks for responding, The code you see is all code for that screen, I would think according to some evidence that the problem is in the response times of jobs, if you look carefully you will see that within one job.succes I open another job.
     
  15. walterf25

    walterf25 Well-Known Member Licensed User

    I really think the listview stuff should be handled in the
    Code:
    Sub Handle_lst_tragos(job as httpjob)
    'code to handle the items that must go in the list view should go here, once this is completed you can start the next httpjob
    End sub
     
  16. tamayo461

    tamayo461 Member Licensed User

    We agree, the problem is in the nested jobs. I will modify the code so that jobs are made to order. Thank You
     
  17. jcesar

    jcesar Active Member Licensed User

    Its true. I'm using the B4A Imagedownloader with B4i.

    See the attachment.
     

    Attached Files:

  18. tamayo461

    tamayo461 Member Licensed User

    ;) Thank you
     
  19. tamayo461

    tamayo461 Member Licensed User

    Ok, this is the code that downloads the images and then the text to display on a CustomListView, the problem is that downloading photos is in disarray and do not match the text, anyone have any idea? see img...

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Private lst_tragos = "lst_tragos", fot_marca = "fot_marca", fot_pro = "fot_pro", fotos = "fotos"
        
    Dim lv As CustomListView
        
    Type treslineas (First As String, Second As String, Tercero As String)
        
    'Dim temp As Boolean
        Private pg As Page
        
    Private Label1 As Label
        
    Private ncarrito As Label
        
    Private carrito As ImageView
        
    Private foto_marca As ImageView
        
    Dim imgmarca,cod_foto As String
        
    Dim list_ima, list_ima2 As List
        
    Type list_ima_col(id As Int, tag As String, img As Bitmap)
        
    Type list_ima2_col(id As Int, tag As String, img As Bitmap)
        
    Dim list_imag As Map
        
    Dim cont, contfot As Int
        
    Private desc As Label
        
    Private atras As ImageView

    End Sub

    Public Sub Show
        
    If pg.IsInitialized = False Then
            pg.Initialize(
    "pg")
            pg.RootPanel.LoadLayout(
    "carta")
        
    End If
        Main.NavControl.ShowPage(pg)
        list_ima.Initialize
        list_ima2.Initialize
        list_imag.Initialize
        
    Dim Bm As Bitmap
        Bm.Initialize(
    File.DirAssets,"ic_action_refresh.png")
        foto_marca.Bitmap = Bm
        desc.Initialize(
    "")

        lv.Clear
        traer_fotos 
    '<-----call the work that brings photos and stored in a list
    End Sub

    Sub cargar_img(fot As String)
        des_img(
    "http://www.xxxxxx.com/app/ejxxxes/ixxxges/marca/" & fot, fot_marca)
    End Sub

    Sub traer_fotos
        ExecuteRemoteQuery(
    "select * from inxxxxtario where categoria = '"&Main.categoria&"' and marca = '"&Main.marca&"'", fotos)
    End Sub

    Sub llenar_lista
        ExecuteRemoteQuery(
    "select * from xxxxxo where categoria = '"&Main.categoria&"' and marca = '"&Main.marca&"'", lst_tragos)
    End Sub

    Sub ExecuteRemoteQuery(Query As String, JobName As String)
        
    Dim job As HttpJob
        job.Initialize(JobName, Me)
        job.PostString(
    "http://www.dxxxxx.com/netdbxxxxores.php", Query)
    End Sub

    Sub des_img(url As String, JobName As String)
        
    Dim job As HttpJob
        job.Initialize(JobName, Me)
        job.Download(url)
    End Sub

    Sub JobDone(Job As HttpJob)
        
    'ProgressDialogHide
        If Job.Success Then   
            
    'Log("Response from server: " & res)   
            Select Job.JobName
            
    '***************************************************************************************************************
                Case fotos '<-----Case Photo
                    Dim res As String
                    res = Job.GetString
                    
    Dim parser As JSONParser
                    parser.Initialize(res)
                    
    Dim l2 As List
                    l2 = parser.NextArray
                    cont = l2.Size
                    
    If l2.Size = 0 Then
                        
    Msgbox("No se encontraron registros"False)
                    
    Else
                    
    Log ("Tamaño del rs: "&l2.Size)
                    contfot = 
    0
                    cod_foto = 
    ""
                        
    For i = 0 To l2.Size - 1'<-----For that runs the sql result to know the name of the photo and call the function that discharge           
                            Dim tl As treslineas
                            
    Dim m As Map
                            m = l2.Get(i)
                            tl.First = m.Get(
    "nombre")
                            tl.Second = m.Get(
    "foto")
                            imgmarca = m.Get(
    "foto_marca")
                            cod_foto = tl.First
                            
    Log("Desde el for: "&cod_foto)                   
                            des_img(
    "http://www.dxxxxxxx.com/app/ejxxxxres/images/tragos/" & tl.Second, fot_pro)'<-----function callback download images                   
                        Next
                        cargar_img(imgmarca)
    '<-----Call the function that discharge prevention image of Cabora screen
                    End If
            
    '***************************************************************************************************************
                Case lst_tragos'<-----Case txt
                    If lv.IsInitialized = False Then
                        lv.Initialize(Me, 
    "lv"100%X)
                        pg.RootPanel.AddView(lv.AsView, 
    00100%x150%y)
                        lv.SetSize(Label1.Width, 
    150%y)
                        lv.AsView.Left = 
    0%x
                        lv.AsView.Top = 
    0%y + foto_marca.Top + foto_marca.Height + 3dip
                        lv.AsView.Color = 
    Colors.ARGB(25,0,0,0)
                    
    End If           
                    
    Dim res As String
                    res = Job.GetString
                    
    Dim parser As JSONParser
                    parser.Initialize(res)
                    
    Dim l2 As List
                    lv.Clear           
                    l2 = parser.NextArray
                    
    If l2.Size = 0 Then
                        
    Msgbox("No se encontraron registros"False)
                    
    Else                           
                        
    For i = 0 To l2.Size - 1                   
                            
    Dim tl As treslineas
                            
    Dim m As Map
                            m = l2.Get(i)
                            tl.First = m.Get(
    "nombre")
                            tl.Second = m.Get(
    "foto")
                            tl.Tercero = m.Get(
    "descripcion")& Chr(13) & Chr(10) & " Valor: $" & m.Get("valor")                   
                            lv.Add(CreateListItem(tl.First,tl.Second , i, 
    120%x70dip), 70dip"Item")
                        
    Next           
                    
    End If
            
    '***************************************************************************************************************
                Case fot_marca
                    foto_marca.Bitmap=Job.GetBitmap
            
    '***************************************************************************************************************
                Case fot_pro       
                    
    Log("-> Fot_pro  contfot: "&contfot & " Tag: " & Job.Tag & " BitMap: " & Job.GetBitmap)
                    
    Dim p As list_ima_col
                    p.id = contfot
                    p.tag = Job.Tag
                    p.img = Job.GetBitmap           
                    list_ima.Add(p)
                    contfot = contfot + 
    1           
                    
    If (contfot = cont) Then'<-----After you download all photos I call the function to download the data to match the photos
                        list_ima.SortType("id",True)'<-----ordered list of photos downloaded as recommended Erel
                        For i = 0 To list_ima.Size - 1
                            
    Dim q As list_ima_col
                            q = list_ima.Get(i)   
                            list_ima2.InsertAt(i,q)
    '<-----ordered after I copy a new list
                            Log("Tag: " & q.tag & " Bitmap: " & q.img)
                        
    Next
                        llenar_lista
                    
    End If
                
    End Select   
        
    Else
            
    Msgbox("Error: " & Job.ErrorMessage, True)
        
    End If
        Job.Release
    End Sub

    Sub CreateListItem(Text As String,foto As String, indx As String, Width As Int, Height As Int) As Panel
        
    Dim p As Panel
        p.Initialize(
    "")
        p.Color = 
    Colors.Black
        
    Dim q As list_ima2_col
        q = list_ima2.Get(indx)
        
    Dim b As Bitmap    = q.img
        
    Dim im As ImageView
        im.Initialize(
    "")
        im.Bitmap = b
        
    Dim lbl As Label
        lbl.Initialize(
    "lbl")
        lbl.TextAlignment = lbl.ALIGNMENT_LEFT
        lbl.Text = Text
        lbl.Font = 
    Font.CreateNew(13)
        lbl.TextColor = 
    Colors.White
        p.AddView(lbl, Height+
    5dip,0,Width*0.8,Height)
        p.AddView(im, 
    0,0,Height,Height)
        
    Return p
    End Sub
     

    Attached Files:

    Last edited: Feb 13, 2015
  20. tamayo461

    tamayo461 Member Licensed User

    ok I got it, this code works perfectly downloading the images on a server, the name of the images is a list that gives me the server as a result of a SQL query, the images are first downloaded and stored with the respective index, then resulting data in SQL. all in the same class.

    The problem I had was that initially did not coincide images and data in the CustomListView, what siolucione with Job.GetRequest and performing basic text to extract the name of the image decargada to serve me in the list of index images

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.   
        Private lst_tragos = "lst_tragos", fot_marca = "fot_marca", fot_pro = "fot_pro", fotos = "fotos"
        
    Dim lv As CustomListView   
        
    Type treslineas (First As String, Second As String, Tercero As String)
        
    'Dim temp As Boolean   
        Private pg As Page   
        
    Private Label1 As Label
        
    Private ncarrito As Label
        
    Private carrito As ImageView
        
    Private foto_marca As ImageView
        
    Dim imgmarca As String
        
    Dim list_ima As List
        
    Type list_ima_col(tag As String, img As Bitmap)
        
    'Type list_ima2_col(id As Int, tag As String, img As Bitmap)
        'Dim list_imag As Map
        Dim cont, contfot As Int    
        
    Private desc As Label
        
    Private atras As ImageView   
       
    End Sub

    Public Sub Show
        
    If pg.IsInitialized = False Then
            pg.Initialize(
    "pg")
            pg.RootPanel.LoadLayout(
    "carta")
        
    End If
        Main.NavControl.ShowPage(pg)   
        list_ima.Initialize   
        
    Dim Bm As Bitmap
        Bm.Initialize(
    File.DirAssets,"ic_action_refresh.png")
        foto_marca.Bitmap = Bm
        desc.Initialize(
    "")       
        lv.Clear   
        traer_fotos 
    '<-----call the work that brings photos and stored in a list   
    End Sub

    Sub cargar_img(fot As String)
        des_img(
    "http://www.xxxxx.com/app/xxxes/images/marca/" & fot, fot_marca)
    End Sub

    Sub traer_fotos
        ExecuteRemoteQuery(
    "select foto,foto_marca from ixxxxxo where categoria = '"&Main.categoria&"' and marca = '"&Main.marca&"'", fotos)
    End Sub

    Sub llenar_lista
        ExecuteRemoteQuery(
    "select * from inxxxrio where categoria = '"&Main.categoria&"' and marca = '"&Main.marca&"'", lst_tragos)
    End Sub

    Sub ExecuteRemoteQuery(Query As String, JobName As String)
        
    Dim job As HttpJob
        job.Initialize(JobName, Me)
        job.PostString(
    "http://www.xxxxxxx.com/nexxb_ejx.php", Query)
    End Sub

    Sub des_img(url As String, JobName As String)
        
    Dim job As HttpJob
        job.Initialize(JobName, Me)   
        job.Download(url)   
    End Sub

    Sub JobDone(Job As HttpJob)   
        
    'ProgressDialogHide
        If Job.Success Then   
            
    'Log("Response from server: " & res)       
            Select Job.JobName   
            
    '***************************************************************************************************************
                Case fotos '<-----Case Photo
                    Dim res As String
                    res = Job.GetString
                    
    Dim parser As JSONParser
                    parser.Initialize(res)   
                    
    Dim l2 As List
                    l2 = parser.NextArray
                    cont = l2.Size
                    
    If l2.Size = 0 Then
                        
    Msgbox("No se encontraron registros"False)
                    
    Else               
                        contfot = 
    0                   
                        
    For i = 0 To l2.Size - 1'<-----For that runs the sql result to know the name of the photo and call the function that discharge               
                            Dim tl As treslineas
                            
    Dim m As Map
                            m = l2.Get(i)
                            
    'tl.First = m.Get("nombre")   
                            tl.Second = m.Get("foto")
                            imgmarca = m.Get(
    "foto_marca")   
                            
    'cod_foto = tl.First   
                            'Log("Desde el for: "&cod_foto)
                            des_img("http://www.xxxxx.com/app/ejelicores/images/tragos/" & tl.Second, fot_pro)'<-----function callback download images                       
                        Next
                        cargar_img(imgmarca)
    '<-----Call the function that discharge prevention image of Cabora screen
                    End If
            
    '***************************************************************************************************************
                Case fot_pro   
                    
    Dim tx As String '<----- extract the name of the photo from the result given me Job.GetRequest
                    tx = Job.GetRequest
                    tx = tx.SubString2(tx.IndexOf(
    "{"),tx.IndexOf("}"))       
                    tx = tx.SubString2(
    58,tx.Length-1)'<-----58 are the initial characters of the url set before me the name of the image to be downloaded
                    Dim p As list_ima_col
                    p.tag = tx
    '<----- index
                    p.img = Job.GetBitmap'<----- image           
                    list_ima.Add(p)
                    contfot = contfot + 
    1               
                    
    If (contfot = cont) Then'<-----After you download all photos I call the function to download the data to match the photos
                        llenar_lista
                    
    End If       
            
    '***************************************************************************************************************
                Case fot_marca
                    foto_marca.Bitmap=Job.GetBitmap
            
    '***************************************************************************************************************
                Case lst_tragos'<-----Case txt
                    If lv.IsInitialized = False Then
                        lv.Initialize(Me, 
    "lv"100%X)
                        pg.RootPanel.AddView(lv.AsView, 
    00100%x150%y)
                        lv.SetSize(Label1.Width, 
    150%y)
                        lv.AsView.Left = 
    0%x
                        lv.AsView.Top = 
    0%y + foto_marca.Top + foto_marca.Height + 3dip
                        lv.AsView.Color = 
    Colors.ARGB(25,0,0,0)
                    
    End If               
                    
    Dim res As String
                    res = Job.GetString
                    
    Dim parser As JSONParser
                    parser.Initialize(res)   
                    
    Dim l2 As List
                    lv.Clear               
                    l2 = parser.NextArray
                    
    If l2.Size = 0 Then
                        
    Msgbox("No se encontraron registros"False)
                    
    Else                               
                        
    For i = 0 To l2.Size - 1                       
                            
    Dim tl As treslineas
                            
    Dim m As Map
                            m = l2.Get(i)
                            tl.First = m.Get(
    "nombre")
                            tl.Second = m.Get(
    "foto")
                            tl.Tercero = m.Get(
    "descripcion")& Chr(13) & Chr(10) & " Valor: $" & m.Get("valor")                       
                            lv.Add(CreateListItem(tl.First,tl.Second , i, 
    120%x70dip), 70dip"Item")
                        
    Next               
                    
    End If   
                
    End Select
        
    Else
            
    Msgbox("Error: " & Job.ErrorMessage, True)
        
    End If   
        Job.Release
    End Sub

    Sub CreateListItem(Text As String,foto As String, indx As String, Width As Int, Height As Int) As Panel
        
    Dim p As Panel
        p.Initialize(
    "")
        p.Color = 
    Colors.Black
        
    Dim q As list_ima_col    
        
    For i = 0 To contfot-1
            q = list_ima.Get(i)
            
    If (q.tag = foto) Then
                
    Dim b As Bitmap    = q.img           
            
    End If
        
    Next
        
    Dim im As ImageView
        im.Initialize(
    "")
        im.Bitmap = b   
        
    Dim lbl As Label
        lbl.Initialize(
    "lbl")
        lbl.TextAlignment = lbl.ALIGNMENT_LEFT   
        lbl.Text = Text
        lbl.Font = 
    Font.CreateNew(13)
        lbl.TextColor = 
    Colors.White
        p.AddView(lbl, Height+
    5dip,0,Width*0.8,Height)
        p.AddView(im, 
    0,0,Height,Height)   
        
    Return p
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice