Android Question VisibleRangeChanged problems with imagedownloader

Carlos marin

Active Member
Licensed User
Longtime User
hello experts I'm looking in the forums to clear a doubt and I can not find an answer.
with this code I download images from the server but I do not know how to fit it to my code.
B4X:
links.Put(img2, "http://www.miweb.com/solodomi/aliados/"&id_emp&"/productos/"&cd.id&".png")
CallSubDelayed2(ImageDownloader, "Download", links)

i use VisibleRangeChanged to load my customlistview

B4X:
Private Sub CreateItem(Width As Int, Title As String, id  As String, nombre  As String, descripcion  As String, precio  As String, items  As String )
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 155dip
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 170dip
    Dim cd As CardData
    cd.Initialize
    cd.Title = Title
    cd.id = id
    cd.nombre = nombre
    cd.descripcion = descripcion
    cd.precio = precio
    cd.items = items
    p.SetLayoutAnimated(0, 0, 0, Width, height)
    CLV.Add(p, cd)
End Sub

B4X:
Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 20
   
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = CLV.GetValue(i)
                p.LoadLayout("CellItem2")
                lblnombre.Text = cd.nombre
                lbldescripcion.Text = cd.descripcion
                lblprecio.Text = cd.precio
                If miclase.pantalladip = "Gama-baja" Then
                    lblnombre.TextSize = 10
                    lbldescripcion.TextSize = 9
                    lblprecio.TextSize = 13
                    lblpeso.TextSize = 12
                End If
               ImgCpanel.SetBackgroundImage(LoadBitmapSample(File.DirAssets,"PnlProdMesa.png",25%x,25%y))
                btnmas.Tag = cd.id & "-" & cd.nombre & "-" & cd.descripcion & "-" & cd.precio & "-" & cd.items
                img2.Tag = cd.id & "-" & cd.nombre  & "-" & cd.descripcion & "-" & cd.precio & "-" & cd.items
                img2.Visible = True
                'CallSubDelayed2(ImageDownloader, "Download", links)
                fuente
            End If
        Else
            'Not visible
            If p.NumberOfViews > 0 Then
                p.RemoveAllViews
            End If
        End If
    Next
End Sub

where I insert the code ImageDownloader?, what is the correct and optimized way? any help
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello @Carlos marin,
That's all wrong for integrating lazy loading.

You do not need the 'CreateItem Sub' sub anymore, I suggest that you read Erel's tutorial again. You only need to integrate a part of the original 'CreateItem Sub' into 'FillList2 Sub'.

Enjoy...
 
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
Hello Peter, I do not understand what you are saying, I did it following this tutorial, the only thing I need to know is how to upload images from the Internet. I use the imagedownloader. and I tried to adapt it in filllist2 without success
:(:(:( (my CreateItem = FillList2 if you mean that)
B4X:
 links.Put (img2, "[URL]http://www.miweb.com/solodomi/aliados/[/URL]"&id_emp&"/productos/"&cd.id&".png")
CallSubDelayed2 (ImageDownloader, "Descargar", enlaces)
 
Last edited:
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
I tried this but I do not get the result I need
B4X:
Type CardData ( bm As ImageView)
Sub FillList2
    '...code
    cd.bm.Initialize("")
    cd.bm.Width = 130dip
    cd.bm.Height = 100dip
    cd.bm.Bitmap = LoadBitmapSample(File.DirAssets, "icofood2.jpg",15%x,15%y)
    links.Put(cd.bm, "http://www.miweb.com/solodomi/aliados/"&id_emp&"/productos/"&cd.id&".png")
End Sub
now in the VisibleRangeChanged I call the downloader
B4X:
Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 10
 
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = CLV.GetValue(i)
                '**************** this code is similar to the code in CreateItem from the

'code......
     
                CallSubDelayed2(ImageDownloader, "Download", links)
      
'code.......
            End If
        Else
            'Not visible
            If p.NumberOfViews > 0 Then
                p.RemoveAllViews
            End If
        End If
    Next
End Sub
and this are the results... (see pictures)
only uploads some images, when I scroll, is this the correct way?
 

Attachments

  • WhatsApp Image 2018-06-29 at 6.06.56 PM.jpeg
    WhatsApp Image 2018-06-29 at 6.06.56 PM.jpeg
    97.5 KB · Views: 419
  • WhatsApp Image 2018-06-29 at 6.06.55 PM.jpeg
    WhatsApp Image 2018-06-29 at 6.06.55 PM.jpeg
    110.1 KB · Views: 303
Last edited:
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
I recommend you to start with a simpler approach. Create all items in Activity_Create.

Use VisibleRangeChanged to load the bitmaps. You can also remove invisible bitmaps. Make sure to call Job.GetBitmapResize to avoid loading large images. You might need to modify ImageDownloader for this.

thanks Erel for answering. some example of how to load the bitmap?, the way I do it is not correct, but I do not see in any post how to do it. I have no idea how to do it :(:(:( (all the Items I bring from a query from mysql server)

and now i try this!

B4X:
Type CardData ( bm As ImageView)
Sub FillList2
    '...code
    cd.bm.Initialize("")
    cd.bm.Bitmap = LoadBitmapSample(File.DirAssets, "icofood2.jpg",15%x,15%y)
    links.Put(cd.bm, "http://www.miweb.com/solodomi/aliados/"&id_emp&"/productos/"&cd.id&".png")
    CallSubDelayed2(ImageDownloader, "Download", links)
    p.SetLayoutAnimated(0, 0, 0, Width, height)

End Sub

in the VisibleRangeChanged
B4X:
Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 10
 
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = CLV.GetValue(i)
                '**************** this code is similar to the code in CreateItem from the

'code......
   
                img2.Bitmap = cd.bm.Bitmap
     
'code.......
            End If
        Else
            'Not visible
            If p.NumberOfViews > 0 Then
                p.RemoveAllViews
            End If
        End If
    Next
End Sub
the view is very stable but the images are only shown when I scroll down.
what is missing me?? o_Oo_Oo_O
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is how I would have done it:
1. Remove ImageDownloader. You don't need it.
2.
B4X:
Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 10
 
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = CLV.GetValue(i)
                '**************** this code is similar to the code in CreateItem from the

'code......
                Dim j As HttpJob
                Dim iv As ImageView = img2 'must be a local variable
                j.Initialize("", Me)
                j.Download(...)
                Wait For (j) JobDone(j As HttpJob) 
                if j.Success Then
 iv.Bitmap = j.GetBitmapResize(...)
 End If
 j.Release
                
     
'code.......
            End If
        Else
          'start simple. Don't remove anything
        End If
    Next
End Sub
 
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
This is how I would have done it:
1. Remove ImageDownloader. You don't need it.
2.
B4X:
Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 10
 
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = CLV.GetValue(i)
                '**************** this code is similar to the code in CreateItem from the

'code......
                Dim j As HttpJob
                Dim iv As ImageView = img2 'must be a local variable
                j.Initialize("", Me)
                j.Download(...)
                Wait For (j) JobDone(j As HttpJob)
                if j.Success Then
 iv.Bitmap = j.GetBitmapResize(...)
 End If
 j.Release
               
    
'code.......
            End If
        Else
          'start simple. Don't remove anything
        End If
    Next
End Sub

It was just what I needed, Erel thank you very much! you're the best ;):D:D:D:D:D
 
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
Hi Experts. working with VisibleRangeChanged and the code that Erel gave me, two things happen to me (when the user leaves the view quickly)
1. HttpJob takes a while to complete if the user leaves the activity, when HttpJob completes this it returns me to the original activity
2. I get an error
B4X:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
my solution but sometimes the application stops with this error(see image). Any suggestions?
B4X:
StopService(HttpUtils2Service)
 

Attachments

  • imagen.png
    imagen.png
    141.2 KB · Views: 301
Upvote 0

Carlos marin

Active Member
Licensed User
Longtime User
Hi Erel this the error code:
B4X:
line 633: topService(HttpUtils2Service)

Error ocurred on line:633(productos)
java.lang.ClassCastException: anywheresoftware.b4a.samples.httputils2.httpjob cannot be cast to android.content.Intent
the user always leaves the view quickly this calls the event Activity_KeyPress how to control from here?
 
Last edited:
Upvote 0
Top