Android Question Add image to clv

juniorteacher

Member
Licensed User
Longtime User
how to make customlistview with image from the server,?

i try with job.getbitmap and createlistitem but i still get error and confuse.
 

juniorteacher

Member
Licensed User
Longtime User
my panel,

B4X:
Sub CreateListItem(itemnama As String,itemket As String,itemgmb As Bitmap) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 106dip)
    p.LoadLayout("itemlayout")
    fototanaman.Bitmap = itemgmb
    lblNama.Text = itemnama
    lblKet.Text = itemket
    Return p
End Sub

jobdone event
B4X:
Case "datatanaman"
                'Log(Job.GetString)
                Dim parser As JSONParser
                parser.Initialize(Job.GetString)
                Dim root As List = parser.NextArray
                clv1.Clear           
                For Each colroot As Map In root
                    Dim no2 As Int = colroot.Get("no")
                    Dim keterangan2 As String = colroot.Get("keterangan")
                    Dim nama2 As String = colroot.Get("nama")
                    Dim foto2 As String = colroot.Get("foto")
                    Dim id2 As Int = colroot.Get("id")
                    Dim id2_user As String = colroot.Get("id_user")
                    Dim lat2 As String = colroot.Get("lat")
                    Dim long2 As String = colroot.Get("long")
                    gambartanaman(id2)
                    clv1.Add(CreateListItem(nama2,keterangan2,images2(id2)),106dip,id2)
                    ProgressDialogHide
                Next

sub event
B4X:
Sub posttdatatanaman
    datatanaman.Initialize("datatanaman",Me)
    datatanaman.PostString("http://xxx.xxx/sqldata.php", "key=" & Main.key1 & "&tipe=datatanaman"  )
    ProgressDialogShow("Loading Data Tanaman...")
End Sub

Sub gambartanaman(idfoto As Int)
    datatanaman.Initialize("ambilgambar",Me)
    datatanaman.Download("http://xxx.xxx/datagambar/" & idfoto &".jpg")
    datatanaman.Tag = idfoto
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        images2(j.Tag) = j.GetBitmap
    End If
    j.Release
End Sub

my array image
B4X:
Dim images2() As Bitmap

B4X:
Logger connected to:  asus ASUS_Z00AD
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (dashboard) Create, isFirst = true **
** Activity (dashboard) Resume **
** Activity (dashboard) Pause, UserClosed = true **
** Activity (data) Create, isFirst = true **
** Activity (data) Resume **
datatanaman
Error occurred on line: 240 (Data)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=29
    at example.navigationview.data._jobdone(data.java:1020)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5480)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (dashboard) Create, isFirst = true **
** Activity (dashboard) Resume **
** Activity (dashboard) Pause, UserClosed = true **
** Activity (data) Create, isFirst = true **
** Activity (data) Resume **
datatanaman
Error occurred on line: 240 (Data)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=28
    at java.lang.reflect.Array.get(Array.java:72)
    at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
    at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:568)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:374)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5480)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Dim images2() As Bitmap
You're declaring a 0 element array
images2(j.Tag) = j.GetBitmap
You're putting something into an array at position j.Tag, even though the array has been declared as a 0 element array

Since the # of elements is unknown, you could use a map instead. You have to then adjust your code to use the map
B4X:
Dim images2 As Map
images2.Initialize
B4X:
If j.Success Then
    images2.Put(j.Tag, j.GetBitmap)
End If
B4X:
clv1.Add(CreateListItem(nama2,keterangan2,images2.Get(id2)),106dip,id2)
and anywhere else that you access the images2 array in your code.
 
Upvote 0

juniorteacher

Member
Licensed User
Longtime User
yes it works, thanks

but my code still error with different information
the error from CreateListItem
B4X:
Sub CreateListItem(itemnama As String,itemket As String,itemgmb As Bitmap) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 106dip)
    p.LoadLayout("itemlayout")
    fototanaman.Bitmap = itemgmb <- error this line
    lblNama.Text = itemnama
    lblKet.Text = itemket
    Return p
End Sub


B4X:
Logger connected to:  asus ASUS_Z00AD
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (dashboard) Create, isFirst = true **
** Activity (dashboard) Resume **
** Activity (dashboard) Pause, UserClosed = true **
** Activity (data) Create, isFirst = true **
** Activity (data) Resume **
datatanaman
Error occurred on line: 296 (Data)
java.lang.RuntimeException: Object should first be initialized (Bitmap).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5480)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (dashboard) Create, isFirst = true **
** Activity (dashboard) Resume **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (dashboard) Resume **
** Activity (dashboard) Pause, UserClosed = true **
** Activity (data) Create, isFirst = true **
** Activity (data) Resume **
Ignoring event: navdrawer_drawerclosed
datatanaman
Error occurred on line: 296 (Data)
java.lang.RuntimeException: Object should first be initialized (Bitmap).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at example.navigationview.data._createlistitem(data.java:649)
    at example.navigationview.data._jobdone(data.java:1023)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5480)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

juniorteacher

Member
Licensed User
Longtime User
Where and how (what is it?) is this variable declared?
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private VP As AHViewPager
    Private ToolBar As ACToolBarLight
    Private NavDrawer As DSNavigationDrawer
    Private Switch As ACSwitch
    Dim closeCntr As Int= 0
    Private Label1 As Label
    Private Label2 As Label
    Dim datatanaman As HttpJob
    Private clv1 As CustomListView
    Private lblNama As Label
    Private lblKet As Label
    Private fototanaman As ImageView
    Private btnEdit As Button
    Private btnHapus As Button
    Private Panel1 As Panel
    Private dataform As Panel
    Private DetailsDialog As CustomLayoutDialog
    Private DSFloatingActionButton1 As DSFloatingActionButton
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
    Private txttanaman As EditText
    Private txtketerangan As EditText
    Dim up As UploadFilePhp
    Dim images2 As Map
End Sub

i create "fototanaman" from designer layout
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It looks like GetBitmap is returning an un-initialized Bitmap. Right now, the code just assumes that a valid Bitmap is returned.
Change
B4X:
datatanaman.Download("http://xxx.xxx/datagambar/" & idfoto &".jpg")
to
B4X:
Dim bitmapURL as String = "http://xxx.xxx/datagambar/" & idfoto &".jpg"
datatanaman.Download(bitmapURL)
and
B4X:
If j.Success Then
    images2.Put(j.Tag, j.GetBitmap)
End If
with the code below and see what is logged.
B4X:
If j.Success Then
    Dim aBitmap as Bitmap = j.GetBitmap
    If (aBitmap.IsInitialized = True) Then
        images2.Put(j.Tag, j.GetBitmap)
    Else
        log("No valid bitmap returned from " & bitmapURL)
        log(j.GetString)
    Endif
Else
    log("Error accessing " & bitmapURL)
    log(j.ErrorMessage)
End If
If you get a bunch of "No valid bitmap" error messages, check the URL that the message prints out and see if it a valid URL.
 
Upvote 0
Top