Android Question Working with CustomListView, which also contains CustomListView

red30

Active Member
Licensed User
I have a CustomListView "allclv" which contains several "Photo" Layouts. Layout "Photo" contains several buttons and one more CustomListView "clvPhoto".
Layout «Photo».png
How can I add the selected photo to the clvPhoto where the button was clicked?
Here is my code. He does not work.
B4X:
Sub btnAddPhoto_Click
    Dim index As Int = allclv.GetItemFromView(Sender) 
    indexAddPhoto=index
  
    Dim i As Intent
    i.Initialize("android.intent.action.GET_CONTENT", "")
    i.PutExtra("android.intent.extra.ALLOW_MULTIPLE", True)
    i.SetType("image/*")
    StartActivityForResult(i)
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
    If Args(0) = -1 Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        Dim jo As JavaObject = i
        'Android 4.1+ (API 16)
        Dim uris As List
        uris.Initialize
        Dim clipdata As JavaObject = jo.RunMethod("getClipData", Null)
        If clipdata.IsInitialized Then
            Dim count As Int = clipdata.RunMethod("getItemCount", Null)
            For i2 = 0 To count -1
                Dim item As JavaObject = clipdata.RunMethod("getItemAt", Array(i2))
                uris.Add(item.RunMethod("getUri", Null))
            Next
        Else
            uris.Add(i.GetData)
        End If
        Log(uris)
        For j=0 To uris.Size-1
          
            Dim pnl As B4XView = allclv.GetPanel(indexAddPhoto).GetView(1)
            Dim MyCLV As CustomListView = pnl
          
            Dim p As B4XView = Main.xui.CreatePanel("")
            p.SetLayoutAnimated(0, 0, 0, allclv.AsView.Width, Activity.Height)
            p.LoadLayout("ImageView")
            Dim btm As Bitmap = LoadBitmap("ContentDir",uris.Get(j))
            iv.Bitmap=btm
            MyCLV.Add(p,"")
        Next
    End If
    Return Null
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub
Perhaps there is another way. I need an element in the CustomListView that I can scroll horizontally and add a photo there, as well as delete the selected photo.
I'll try to describe it in more detail to make it clearer. I have a CustomListView which contains a panel where the photo is displayed, as well as two buttons to add and take a photo. There will be about 10 such panels. I need the panel where the photos will be able to scroll horizontally and contain many photos. Also, if I click on the photo, I could delete it.
 
Last edited:

red30

Active Member
Licensed User
This code cannot work:
B4X:
Dim pnl As B4XView = allclv.GetPanel(indexAddPhoto).GetView(1)
Dim MyCLV As CustomListView = pnl
It is explained here: [B4X] How to get <custom view here> from <CLV or any other container>
I don't understand how to use it ... If I do this:
B4X:
            Dim MyCLV As CustomListView = allclv.GetPanel(indexAddPhoto).GetView(1).Tag
            
            Dim p As B4XView = Main.xui.CreatePanel("")
            p.SetLayoutAnimated(0, 0, 0, clvPhoto.AsView.Width/3, clvPhoto.AsView.Height)
            p.LoadLayout("ImageView")
            iv.Bitmap=xui.LoadBitmapResize("ContentDir", uris.Get(j), iv.Width, iv.Height, True)
            MyCLV.Add(p,"")
I get an error:
B4X:
Error occurred on line: 121 (Mod2)
java.lang.RuntimeException: Method: _add not found in: java.lang.String
    at anywheresoftware.b4a.shell.Shell$MethodCache.getMethod(Shell.java:985)
    at anywheresoftware.b4a.shell.Shell.getMethod(Shell.java:644)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:774)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4j.object.JavaObject$1.invoke(JavaObject.java:237)
    at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
    at $Proxy0.ResultArrived(Unknown Source)
    at anywheresoftware.b4a.BA$4.run(BA.java:585)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:459)
    at b4a.example.mod2$ResumeMessage.run(mod2.java:313)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6865)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:504)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 
Upvote 0

red30

Active Member
Licensed User
Here is my program code
I wanted to add photos to a horizontal CustomListView or to have the CustomListView vertical and have 4 photos in a row.
 

Attachments

  • testPhotoCLV.zip
    12.4 KB · Views: 96
Upvote 0

red30

Active Member
Licensed User
What is the output of:
B4X:
Log("*****")
For Each v As B4XView In allclv.GetPanel(indexAddPhoto).GetAllViewsRecursive
Log("tag: " & v.Tag)
Next
I get such a log
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
(ArrayList) [content://com.android.providers.media.documents/document/image%3A1451]
*****
tag:
tag:
tag: null
tag: null
tag:
tag:
Error occurred on line: 99 (Main)
java.lang.RuntimeException: Method: _add not found in: java.lang.String
    at anywheresoftware.b4a.shell.Shell$MethodCache.getMethod(Shell.java:985)
    at anywheresoftware.b4a.shell.Shell.getMethod(Shell.java:644)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:774)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4j.object.JavaObject$1.invoke(JavaObject.java:237)
    at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
    at $Proxy0.ResultArrived(Unknown Source)
    at anywheresoftware.b4a.BA$4.run(BA.java:585)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:459)
    at b4a.example.main$ResumeMessage.run(main.java:313)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6865)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:504)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
1. You can remove almost all of this designer script and replace it with simple anchors.

2. As explained in the tutorial, the convention of setting the class instance to the Tag property is new and wasn't added to xCLV.

You need to do it yourself:
B4X:
For i=0 To 10
    allclv.Add(CreatePhoto(i),"")
    clvPhoto.AsView.Tag = clvPhoto
Next
 
Upvote 0

red30

Active Member
Licensed User
1. You can remove almost all of this designer script and replace it with simple anchors.

2. As explained in the tutorial, the convention of setting the class instance to the Tag property is new and wasn't added to xCLV.

You need to do it yourself:
B4X:
For i=0 To 10
    allclv.Add(CreatePhoto(i),"")
    clvPhoto.AsView.Tag = clvPhoto
Next
1. Why is it preferable? Could you please share an example of how this works.

2. Many thanks!! It really works. It took me a week to figure this out. You helped me a lot. Thanks!
Explain why this should be done in a loop when I create the CustomListView "allclv" items? I do it once after the loop and it doesn't work.
 
Upvote 0
Top