Android Question Job.GetBitmap and Order Row

MarcoRome

Expert
Licensed User
Hi all. I have this code that reads a record from mysql and returns a string and its link to retrieve image from the site.
Example row 1 i have column "descrizione" and column "immagine"
Record 1:
descrizione = "dog"
immagine = "dog.jpg"
Record 2:
descrizione = "cat"
immagine = "cat.jpg"
etc.
Now all work but order "descrizione" is right and order "immagine" il random.... yes random because isn't order and if you run app more time change order in random mode.
This is code:


B4X:
Case LEGGI_TATTOO_LISTA
'Carico immagini da Server
Dim COUNTRIES As List
Dim m As Map
m.Initialize
COUNTRIES = parser.NextArray 'returns a list with maps
sistemiamo_icone = 1
incrementa_i = -1
Dim totale_righe As Int = COUNTRIES.Size - 1
totale_righe_da_carciare = COUNTRIES.Size - 1
Dim pnl As Panel
For i = 0 To COUNTRIES.Size - 1
m = COUNTRIES.Get(i)
'Per Internet
descrizione_array(i) = m.Get("descrizione")
posizione_i = i
Dim job3 As HttpJob
job3.Initialize("Job3", Me)
job3.Download("http://www.xxxx.com/pictures/" & m.Get("immagine"))
Next
 
Case "Job3"
'Carico immagini da Server
Dim totale_righe As Int = totale_righe_da_carciare
incrementa_i = incrementa_i + 1
 
'Per Internet
lbl_test = descrizione_array(incrementa_i)
Dim bm As Bitmap
bm.Inizialize3(Job.GetBitmap)
........
Any idea ?
Thank you
Marco
 

MarcoRome

Expert
Licensed User
Yes work without problem. Thanks Erel

B4X:
Sub Activity_Create(FirstTime As Boolean)
 
Activity.LoadLayout("1")
 
ii = -1
Dim img As Int = 0  
Dim i As Int
 
For i = 0 To 6
    img = img + 1
    Dim job3 As HttpJob
    job3.Initialize("Job3", Me)
    job3.Download("http://www.xxxxx.eu/example/" & "c" & img & ".jpg")
    job3.Tag = "Immagine c" & img & ".jpg"
Next
End Sub
 
 
 
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job3"
                'show the downloaded image
                ii = ii + 1
                Select ii
                Case 0
                ImageView1.SetBackgroundImage(Job.GetBitmap)
                Label1.Text = Job.Tag
                Case 1
                ImageView2.SetBackgroundImage(Job.GetBitmap)
                Label2.Text = Job.Tag
                Case 2
                ImageView3.SetBackgroundImage(Job.GetBitmap)
                Label3.Text = Job.Tag
                Case 3
                ImageView4.SetBackgroundImage(Job.GetBitmap)
                Label4.Text = Job.Tag
                Case 4
                ImageView5.SetBackgroundImage(Job.GetBitmap)
                Label5.Text = Job.Tag
                Case 5
                ImageView6.SetBackgroundImage(Job.GetBitmap)
                Label6.Text = Job.Tag
                End Select
          
              
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

MarcoRome

Expert
Licensed User
Your current code is not perfect. It will download again and again the same images when the activity is recreated (after an orientation change for example).

Check ImageDownloader. It will be simpler and it will handle the cache for you.
Yes Erel, is only example for test.
Thank you again for solution
 

MarcoRome

Expert
Licensed User
Hi all.

B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
ii = -1
Dim img As Int = 0 
Dim i As Int
For i = 0 To 6
    img = img + 1
    Dim job3 As HttpJob
    job3.Initialize("Job3", Me)
    job3.Download("http://www.xxxxx.eu/example/" & "c" & img & ".jpg")
    job3.Tag = "Immagine c" & img & ".jpg"
Next
End Sub
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job3"
                'show the downloaded image
               Log(job.Tag)         
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
This code work but i have problem with order.
i wait that i have this result:

Immagine c1.pg
Immagine c2.pg
Immagine c3.pg
Immagine c4.pg
Immagine c5.pg
etc.
....

but i have this result:
Immagine c2.jpg
Immagine c3.jpg
Immagine c1.jpg
Immagine c5.jpg
Immagine c4.jpg
etc.
....

Any idea to sort in correct mode ?
Thank you
Marco
 

MarcoRome

Expert
Licensed User
Thanks Don Manfred but my case is different. I havent different jobs.
Per explain better i have this code:

B4X:
Sub JobDone(Job As HttpJob)
    If Job.Success Then
    Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
       
        Case LEGGI_TATTOO_LISTA
                           
                'Carico immagini da Server
                Dim COUNTRIES As List
                Dim m As Map
                m.Initialize
                COUNTRIES = parser.NextArray 'returns a list with maps
                sistemiamo_icone = 1
                incrementa_i = -1
                Dim totale_righe As Int = COUNTRIES.Size - 1
                totale_righe_da_carciare = COUNTRIES.Size - 1
                Dim pnl As Panel
                For i = 0 To COUNTRIES.Size - 1
                    m = COUNTRIES.Get(i)                        
                    'Per Internet
                    posizione_i = i
                    Dim job3 As HttpJob
                    job3.Initialize("Job3", Me)
                    job3.Download("http://www.xxxx.xxx/" & m.Get("immagine"))
                    job3.Tag = m.Get("descrizione")
                    Log(m.Get("descrizione")) ' <---- THIS IS CORRECT IN ORDER BECAUSE I HAVE QUERY... ORDER BY
                       

                Next
               

       
        Case "Job3"
                'Carico immagini da Server
                'Per Internet
                 Log(job.Tag) ' <---- HERE I HAVE DIFFERENT ORDER ( RANDOM, ANY TIME CHANGE ) 
     
        End Select

.....
    End If
    Job.Release
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Thanks Don Manfred but my case is different. I havent different jobs.
Per explain better i have this code:

B4X:
                For i = 0 To COUNTRIES.Size - 1
                    m = COUNTRIES.Get(i)                       
                    'Per Internet
                    posizione_i = i
                    Dim job3 As HttpJob
                    job3.Initialize("Job3", Me)
                    job3.Download("http://www.xxxx.xxx/" & m.Get("immagine"))
                    job3.Tag = m.Get("descrizione")
                    Log(m.Get("descrizione")) ' <---- THIS IS CORRECT IN ORDER BECAUSE I HAVE QUERY... ORDER BY
You need to check the link in Manfred's previous post. This is the same problem! You are calling several downloads one immediately after the other. They can then arrive in any order (not necessarily the order in which they were requested). To ensure the correct order you need to do this one at a time in the job done event.
 
Top