Android Question CustomListView and ContentChooser

red30

Well-Known Member
Licensed User
Longtime User
I have a CustomListView that contains 10 elements consisting of a button and a horizontal CustomListView2. When I click the button with ContentChooser, I try to upload a photo from the gallery and display it on CustomListView2. How can I do that?
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Chooser As ContentChooser
    Private MyCLV As CustomListView
    Private Button1 As Button
    Private CLV_IV As CustomListView
    Private ImageView1 As ImageView
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("Main")
    Chooser.Initialize("chooser")
    For i = 1 To 10
        MyCLV.Add(CreateListItem("MyText"&i),i)
    Next
End Sub

Sub CreateListItem(Text As String) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("Item")
    Button1.Text = Text
    Return p
End Sub

Sub CreateListItem2(btm As Bitmap) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100dip, 100dip)
    p.LoadLayout("Item_IV")
    ImageView1.Bitmap=btm
    Return p
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    Log("Checked items: " & index)
    Chooser.Show("image/*", "Choose image")
End Sub

Sub chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim index As Int = MyCLV.GetItemFromView(Sender)
        Dim pnl As B4XView = MyCLV.GetPanel(index)
        Dim clv As B4XView= pnl.GetView(1)
        clv.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
    End If
End Sub
B4X:
B4A Version: 8.00
Parsing code.    (0.03s)
Compiling code.    Error
Error compiling program.
Error description: Unknown member: add
Error occurred on line: 77
clv.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
Word: add
How can I add an image from chooser_Result to CustomListView?
 

Attachments

  • CustomListView and ContentChooser.zip
    18.2 KB · Views: 292
Last edited:

red30

Well-Known Member
Licensed User
Longtime User
This line is wrong:
B4X:
Dim clv As B4XView= pnl.GetView(1)

The view returned will never be a CustomListView. It will be the base panel.

Why aren't you calling:
B4X:
CLV_IV.Add(...)
If I use
B4X:
        CLV_IV.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
That image adds to the last (lower) element of the CustomListView. And I need it to be added under the button that I click.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I misunderstood the layout.

The layout named "Item" includes another CLV, right?

B4X:
Sub CreateListItem(Text As String) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("Item")
    Button1.Text = Text
    Button1.Tag = CLV_IV
    Return p
End Sub

Sub Button1_Click
    Dim btn As Button = Sender
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    Log("Checked items: " & index)
    Chooser.Show("image/*", "Choose image")
    Wait For chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim clv As CustomListView = btn.Tag
        clv.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
    End If
End Sub
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I misunderstood the layout.

The layout named "Item" includes another CLV, right?

B4X:
Sub CreateListItem(Text As String) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, 100%x, 150dip)
    p.LoadLayout("Item")
    Button1.Text = Text
    Button1.Tag = CLV_IV
    Return p
End Sub

Sub Button1_Click
    Dim btn As Button = Sender
    Dim index As Int = MyCLV.GetItemFromView(Sender)
    Log("Checked items: " & index)
    Chooser.Show("image/*", "Choose image")
    Wait For chooser_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        Dim index As Int = MyCLV.GetItemFromView(Sender)
        Dim pnl As B4XView = MyCLV.GetPanel(index)
        Dim clv As CustomListView = btn.Tag
        clv.Add(CreateListItem2(LoadBitmapResize(Dir,FileName,100dip,100dip,True)),0)
    End If
End Sub
Yes that's right. In Item there is a CLV under the name CLV_IV.
Tried as your code, gives an error:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Checked items: 0
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
customlistview_getitemfromview (java line: 410)
java.lang.ClassCastException: anywheresoftware.b4a.phone.Phone$ContentChooser cannot be cast to android.view.View
    at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:53)
    at anywheresoftware.b4a.objects.B4XViewWrapper.getParent(B4XViewWrapper.java:122)
    at b4a.example.customlistview._getitemfromview(customlistview.java:410)
    at b4a.example.main$ResumableSub_Button1_Click.resume(main.java:432)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:843)
    at anywheresoftware.b4a.BA$4.run(BA.java:566)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:432)
    at b4a.example.main$ResumeMessage.run(main.java:305)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5281)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
    at dalvik.system.NativeStart.main(Native Method)
 

Attachments

  • Designer.jpg
    Designer.jpg
    171.1 KB · Views: 365
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
You don't need to find the index again. I've updated the code. Once you understand the code you will be able to change it as you need. It is really simple.
Thank you! Everything is working!
And if I click on ImageView1, how can I delete it?
 
Upvote 0
Top