iOS Question Download images and show in Customlistview

jcesar

Active Member
Licensed User
Longtime User
Hi

Im trying download images from a website and insert in a CustomListView but only the last image
was displayed in listview. All the listview panels show the last image.

B4X:
Sub button_click

Dim url(7) As String
    url(0) = "http://www.der.sp.gov.br/img_cameras/name12.jpg"
    url(1) = "http://www.der.sp.gov.br/img_cameras/name11.jpg"
    url(2) = "http://www.der.sp.gov.br/img_cameras/name35.jpg"
    url(3) = "http://www.der.sp.gov.br/img_cameras/name14.jpg"
    url(4) = "http://www.der.sp.gov.br/img_cameras/name15.jpg"
    url(5) = "http://www.der.sp.gov.br/img_cameras/name28.jpg"
    url(6) = "http://www.der.sp.gov.br/img_cameras/name29.jpg"
    For i=0 To url.Length - 1
       job1.Initialize("Job1", Me)
       job1.Download(url(i))
    Next


End sub


Sub JobDone (job As HttpJob)
    
    If job.Success = True Then
        Select job.JobName
            Case "Job1", "Job2"
                clvRodovia.Add(InsertLine("Name","Description",job.GetBitmap),200dip,0)
               
        End Select
    Else
        Log("Error: " & job.ErrorMessage)
        'hd.ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    job.Release
End Sub
 

jcesar

Active Member
Licensed User
Longtime User
Thanks Erel.

It works. But the http result are out of order.

Is there a way to force the same order of results ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to send each new job when the previous one completed. A better solution is to use Job.Tag to store the request index and then use it to correctly add the images.

You can download all images and store the bitmaps in a List with a custom type that includes the job.Tag value (index) and the bitmap. Then call List.SortType and add all images to the CustomListView.
 
Upvote 0

tamayo461

Member
Licensed User
Longtime 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)


B4X:
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, 0, 0, 100%x, 150%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%x, 70dip), 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


upload_2015-2-5_16-13-7.png
 
Last edited:
Upvote 0

tamayo461

Member
Licensed User
Longtime User
You will need to send each new job when the previous one completed. A better solution is to use Job.Tag to store the request index and then use it to correctly add the images.

You can download all images and store the bitmaps in a List with a custom type that includes the job.Tag value (index) and the bitmap. Then call List.SortType and add all images to the CustomListView.

@Erel How send each new job when the previous one completed???
 
Upvote 0
Top