Android Question [Solved] Click in button of Expandable List

asales

Expert
Licensed User
Longtime User
There are 2 examples of Expandable List:
https://www.b4x.com/android/forum/threads/expandable-list-based-on-customlistview.81445/
https://www.b4x.com/android/forum/threads/b4x-xui-expandable-list-based-on-xcustomlistview.86449/

How I can capture the click in the button of the any item of the list and shows the ID of this item?

click2.jpg
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
To do this I had to make some changes.
I used the example of the first link

B4X:
'Main>>Sub Globals>>*** Add This ***
Private Button2 As Button

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    For i = 1 To 20
        '*** Change this ***
        'clv1.Add(CreateItem(Rnd(0xFF000000, 0xFFFFFFFF), "Item #" & i), CollapsedHeight, "")
         clv1.Add(CreateItem(Rnd(0xFF000000, 0xFFFFFFFF), "Item #" & i, i ), CollapsedHeight, "")
    Next
End Sub

'*** Change this ***
'Sub CreateItem(clr As Int, Title As String) As Panel
Sub CreateItem(clr As Int, Title As String, Index As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    Activity.AddView(p, 0, 0, 100%x, ExpandedHeight)
    p.LoadLayout("Item")
   
    '*** Add this ***
    Button2.Tag=Index
   
    p.RemoveView 'remove from parent
    lblTitle.Text = Title
    pnlTitle.Color = clr
    pnlExpanded.Color = ShadeColor(clr)
    p.Tag = False 'collapsed
    Return p
End Sub

'Create this
Sub Button2_Click
    Dim b As Button=Sender
    Log("From button inside panel: " & b.Tag)
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Activity.AddView(p, 0, 0, 100%x, ExpandedHeight)
Note that it is no longer required to add the view to a parent and then remove it (starting from B4A v7.3).
You can call p.SetLayout instead.

@asales you need to start with this example: https://www.b4x.com/android/forum/t...listview-cross-platform-customlistview.84501/
It shows how you can handle events and find the item index.

You don't need to set the tag, which can be problematic as the index might change.

Code from the example:
B4X:
Sub Button1_Click
   Dim index As Int = clv2.GetItemFromView(Sender)
 
Upvote 0
Top