I download 13 small images with their links from a web site.
I use KVS2 to store the bitmaps as values to a file if file is empty (kvs.ListKeys.Size=0 ) in Dir.internal along the links as keys.
I retrieve the images to populate an xCLV from the file..
the only way the xCLV gets populated the first time with kvs2 empty is if I place a Sleep(5000) after the web download finishes and before I populate the xCLV.
Or:
I add this code to the DownloadImage sub without the Wait For pause, which I can live with:
clv.Add(CreateListItem(j.GetBitmapResize(150dip, 150dip,True), clv.AsView.Width, 150dip, Link ), Link)
kvs.PutBitmap(Link, j.GetBitmapResize(150dip, 150dip,True))
I have tried using Wait For at different locations in the code, Can I do away with the Sleep(5000)
Below is the relevant code:
I use KVS2 to store the bitmaps as values to a file if file is empty (kvs.ListKeys.Size=0 ) in Dir.internal along the links as keys.
I retrieve the images to populate an xCLV from the file..
the only way the xCLV gets populated the first time with kvs2 empty is if I place a Sleep(5000) after the web download finishes and before I populate the xCLV.
Or:
I add this code to the DownloadImage sub without the Wait For pause, which I can live with:
clv.Add(CreateListItem(j.GetBitmapResize(150dip, 150dip,True), clv.AsView.Width, 150dip, Link ), Link)
kvs.PutBitmap(Link, j.GetBitmapResize(150dip, 150dip,True))
I have tried using Wait For at different locations in the code, Can I do away with the Sleep(5000)
Below is the relevant code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
kvs.Initialize(File.DirInternal,"MyKvs")
If kvs.ListKeys.Size=0 Then
MyList.Initialize
MyList=Array("Hannibal.jpg", _
"Silas021415.jpg", _
"crossfire061619_3.jpg", "sweden.png", "turkey.png", "mexico.png", "china.png", "canada.png", "bulgaria.png", _
"denmark.png", "brazil.png", "israel.png", "japan.png" )
DownloadImage(MyList)
Sleep(5000) 'clv does not populate on first try unless I have sleep(5000)
PopulateCLV
Else
PopulateCLV
End If
End Sub
Sub DownloadImage(Links As List)
Dim url As String ="http://xxxxx.com/yyyy/Uploads/"
For Each Link As String In Links
Dim j As HttpJob
j.Initialize("", Me)
j.Download(url & Link) 'link here is the image file name, we add to it the url
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
kvs.PutBitmap(Link, j.GetBitmapResize(150dip, 150dip,True))
' Log(j.success)
Else
Log("Error: " & j.ErrorMessage)
ToastMessageShow("Error: " & j.ErrorMessage, True)
End If
j.Release
Next
Log("downloaded files")
End Sub
Sub PopulateCLV
For Each Link As String In kvs.ListKeys
clv.Add(CreateListItem(kvs.GetBitmap(Link), clv.AsView.Width, 150dip, Link ), Link)
Next
End Sub[code]