JSON Listview help

sktanmoy

Active Member
Licensed User
Longtime User
My code is:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim j As JSONParser
   Dim ListView1 As ListView
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Web")
   
   j.Initialize(File.ReadString(File.DirAssets, "a.json"))
   
   Dim m1 As Map = j.NextObject
   Dim L1 As List = m1.Get("items")
   
   For Each m As Map In L1
      ListView1.AddTwoLines(m.Get("item_name"),  "Price: "&m.Get("item_price"))
   Next

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


I've another field in json where an image url is kept for each item. Can be possible to get using m.Get("item_image")

I can download the image using httputil2. So I need your instruction to use the image in ListView1.AddTwoLinesAndBitmap().

Thanks.
 

sktanmoy

Active Member
Licensed User
Longtime User
How?

If you want to use ListView then you will need to wait for the download to complete and then add the item. Another option is to use a class such as CustomListView which allows you to modify existing items.

How can I use the class here? I didn't find any method to add image in the class. :sign0013:
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
I've tried this way:

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim j As JSONParser
   Dim job1 As HttpJob
   Dim ListView1 As ListView
   Dim s As String
   Dim b As Bitmap
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Web")
   
   j.Initialize(File.ReadString(File.DirAssets, "a.json"))
   job1.Initialize("Job1", Me)
   
   Dim m1 As Map = j.NextObject
   Dim L1 As List = m1.Get("items")
   
   For Each m As Map In L1
      s = job1.Download(m.Get("item_imageUrl"))
      b = job1.GetBitmap
      ListView1.AddTwoLinesAndBitmap2( m.Get("item_name"),  "Price: "&m.Get("item_price"), b, m.Get("item_id") )
   Next

End Sub


But I couldn't understand how to save the image file and use that.
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
If you want to use ListView then you should add the items in JobDone.
I know that but couldn't understand what I should do after job success.

B4X:
Sub JobDone(job As HttpJob)

   If job.Success = True Then
      'Now what should I do?
   End If
   
End Sub
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
Sorry

Sorry but I still need your help to pass downloaded bitmap to listview.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Web")
   
   j.Initialize(File.ReadString(File.DirAssets, "a.json"))
   job1.Initialize("Job1", Me)
   
   Dim m1 As Map = j.NextObject
   Dim L1 As List = m1.Get("items")
   
   For Each m As Map In L1
      job1.Download(m.Get("item_imageUrl"))

      ListView1.AddTwoLinesAndBitmap( m.Get("item_name"),  "Price: "&m.Get("item_price"), INeedHelpHereToPassTheDownloadedImage)
   Next

End Sub

Sub JobDone (Job As HttpJob) 

    If Job.Success = True Then
      Job.GetBitmap
      
    End If
    Job.Release
   
End Sub
 
Upvote 0

kkolle

Member
Licensed User
Longtime User
Hi
try this

add a service modul to your project

B4X:
'Service module
Sub Process_Globals
   Dim myHttpRequest As HttpRequest
   Dim myHttpClient As HttpClient

   Dim FileName As String
End Sub

Sub Service_Create
   myHttpClient.InitializeAcceptAll("myHttpClient")
End Sub

Sub Service_Start (StartingIntent As Intent)
 Dim TaskId = 123 
 myHttpRequest.InitializeGet(Url)
 myHttpClient.Execute(EQHttpRequest, TaskId)  
End Sub

Sub myHttpClient_ResponseSuccess (Response As HttpResponse, TaskId As Int)
      Response.GetAsynchronously("ImageResponse", _ 
        File.OpenOutput(File.DirInternal, FileName, False), True, TaskId)
End Sub

Sub ImageResponse_StreamFinish (Success As Boolean, TaskId As Int)
   If Success = False Then
        Msgbox(LastException.Message, "Error")
        Return
    End If
   Dim FilePath As String
   FilePath = File.DirInternal & "/" & FileName   
        CallSub2(myActiviti, "AddToList", FilePaht)
        'if you need to to download next file, start the service again
        StartServiceAt("", DateTime.Now + 30 * 1000, True)
End Sub

Sub myHttpClient_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log("ERROR HTTP REQUEST")
End Su
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
As I wrote before it will be better to use CustomListView in this case. It will allow you to only add the image to the existing items.
Erel, can you please provide me any link for example. I mean to add image, when download will be completed.
I searched but failed to find out.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…