Android Question [SOLVED] Extracting a value from a label in a custom list view clicked by a user

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I'm trying to extract the value from a label that's in the custom list view item that the user clicks.

Based on this thread and coding example, I set up this Type structure to get values from my custom list view when an item is clicked. Later I will be adding more items into the Type structure.

B4X:
Type typListItemValues (LabelSongTitle As Label)

In the custom list view item click is this code:
B4X:
Sub clvSongList_ItemClick (Index As Int, Value As Object)

    Dim iv As typListItemValues = Value
    Log("You clicked song title: " & iv.LabelSongTitle.Text)
End Sub

When I click on one of the custom list view items, I get a cast error in the Logs:
B4X:
** Activity (main) Resume **
*** Service (servicemodule) Create ***
** Service (servicemodule) Start **
main_clvsonglist_itemclick (java line: 427)
java.lang.ClassCastException: java.lang.Integer cannot be cast to b4a.natures.song.main$_typlistitemvalues
    at b4a.natures.song.main._clvsonglist_itemclick(main.java:427)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1082)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1045)
    at b4a.example3.customlistview$ResumableSub_PanelClickHandler.resume(customlistview.java:805)
    at b4a.example3.customlistview._panelclickhandler(customlistview.java:748)
    at b4a.example3.customlistview._panel_click(customlistview.java:735)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7869)
    at android.view.View.performClickInternal(View.java:7838)
    at android.view.View.access$3600(View.java:886)
    at android.view.View$PerformClick.run(View.java:29362)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:8125)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

Looks like I missed something in the tutorial or the thread I got this information from but I'm not sure what I'm missing.

Thanks.
 

cklester

Well-Known Member
Licensed User
How are you building the CLV? It will require something like this:

B4X:
clvSongList.Add(p,value)

where p is the CLV item panel, and value is the typListItemValues for that particular panel.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
How are you building the CLV? It will require something like this:

B4X:
clvSongList.Add(p,value)

where p is the CLV item panel, and value is the typListItemValues for that particular panel.
Thanks for the reply.

I'm building it like this:
B4X:
Sub ImageViewSongList_Click

    Activity.LoadLayout("SongList") ' This layout also contains a custom list view.

    ' Make panel slide down out of site.
    '-----------------------------------
    PanelMenu.SetLayoutAnimated(300, 0, Activity.Height, Activity.Width, _
        Activity.Height)
        
    ' Add 10 items to the list view.
    '-------------------------------
    For intIndex = 0 To 10
        
        ' Adding items to the list view requires a Panel created in code.
        '----------------------------------------------------------------
        Dim p As B4XView = xui.CreatePanel("")

        ' This is the size for the panel and it will be animated.
        '--------------------------------------------------------
        p.SetLayoutAnimated(0, 0, 0, PanelSongList.Width, 120dip)

        ' This tag will be used later in code.
        '-------------------------------------
        p.Tag = intIndex ' Tag is a placeholder for data values.
        
        p.LoadLayout("SongListCard")
        
        Select intIndex
            Case 0
                LabelSongTitle.Text = kvs.Get("Song1Title")
                
            Case 1
                LabelSongTitle.Text = kvs.Get("Song2Title")

            Case 2
                LabelSongTitle.Text = kvs.Get("Song3Title")
        End Select

        ' This is where the item row is added to the list view.
        '------------------------------------------------------
        clvSongList.Add(p,intIndex)
    Next
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
OK, so you are setting the "value" to either 0, 1, or 2 (intIndex in the clvSongList.Add() part).

You probably want to set the text of the label there (example as above).

Or, actually, maybe you just want to set the LabelSongTitle...

B4X:
clvSongList.Add(p,LabelSongTitle)
I tried to replace intIndex with typListItemValues but I get the "Undeclared variable 'typListItemValues' is used before it was assigned any value" error. Can you tell me how to assign each value to it?

Thanks.
 
Upvote 0

cklester

Well-Known Member
Licensed User
Whatever you pass as the value in the

B4X:
clvSongList.Add(p,VALUE)

is what you'll be able to work with. You can pass anything as that VALUE. A string. A custom type. A map. Etc.

Let's say you pass a string. Then you would have this:

B4X:
Sub clvSongList_ItemClick (Index As Int, Value As Object)
    Log("You clicked song title: " & Value)
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for the guidance.

I will mark this thread as solved. Here is the coding that works now.

I changed the Type structure from label to string.

B4X:
Type typListItemValues (LabelSongTitle As String)

B4X:
Sub ImageViewSongList_Click

    Activity.LoadLayout("SongList") ' This layout also contains a custom list view.

    ' Make panel slide down out of site.
    '-----------------------------------
    PanelMenu.SetLayoutAnimated(300, 0, Activity.Height, Activity.Width, _
        Activity.Height)
       
    ' Add 10 items to the list view.
    '-------------------------------
    For intIndex = 0 To 10
       
        Dim typValues As typListItemValues

        ' Adding items to the list view requires a Panel created in code.
        '----------------------------------------------------------------
        Dim p As B4XView = xui.CreatePanel("")

        ' This is the size for the panel and it will be animated.
        '--------------------------------------------------------
        p.SetLayoutAnimated(0, 0, 0, PanelSongList.Width, 120dip)

        ' This tag will be used later in code.
        '-------------------------------------
        p.Tag = intIndex ' Tag is a placeholder for data values.
       
        p.LoadLayout("SongListCard")
       
        Select intIndex
            Case 0
                LabelSongTitle.Text = kvs.Get("Song1Title")
               
            Case 1
                LabelSongTitle.Text = kvs.Get("Song2Title")

            Case 2
                LabelSongTitle.Text = kvs.Get("Song3Title")
        End Select

        typValues.LabelSongTitle = LabelSongTitle.Text
       
        ' This is where the item row is added to the list view.
        '------------------------------------------------------
        clvSongList.Add(p,typValues)
    Next
End Sub

The new coding is the Dim typValues As typListItemValues line and typValues.LabelSongTitle = LabelSongTitle.Text line and the changed Add line to now be
clvSongList.Add(p,typValues)

This is the updated click handler.

B4X:
Sub clvSongList_ItemClick (Index As Int, Value As Object)

    Dim iv As typListItemValues = Value
    Log("You clicked song title: " & iv.LabelSongTitle)
End Sub

Thanks again. ?
 
Upvote 0
Top