Android Question How to get value from custom listview when buttom pressed

Sai Main Seng Kham

Member
Licensed User
i added buttom to my custom listview , and i want to get custom listview item's values as a string when the buttom pressed.

the code are below .


the values are 123 , 456 , 789 .
when the buttom from the first item of customlistview pressed , let toast message box show out "123".
when the buttom from the second item clicked , toast message box show out "456"

how can i make it
please i already search in forum.
i did not fount solution
i just need this on to finish my big project application
help


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 clv As CustomListView
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("Layout1")
    Activity.Title = "Single Label with Button"
    clv.Initialize(Me, "clv")
    clv.AsView.Color = Colors.Transparent
    Activity.AddView( clv.AsView, 0, 0, 100%x, 100%y)
   
   clv.Add(addItemWithButton("click here","here"),"123")
    clv.Add(addItemWithButton("click here","here"),"456")
    clv.Add(addItemWithButton("click here","here"),"789")
End Sub

Sub Activity_Resume

End Sub

Sub clv_ItemClick (Index As Int, Value As Object)
    Msgbox(Value , "")
End Sub

Sub btnItemWithButton_Click 'the buttom in custom listview
     

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

' itemHeight 60dip
Sub addItemWithButton ( itemname As String , btnName As String ) As Panel
    Private pbase As Panel
    pbase.Initialize("")
    pbase.Color = Colors.Transparent
   
    Private lblName As Label
    Private btn As Button
    lblName.Initialize("")
    lblName.Text = itemname
    lblName.TextColor = Colors.DarkGray
    lblName.Gravity = Gravity.CENTER_VERTICAL
    lblName.TextSize = 13
    pbase.AddView( lblName, 15dip, 0, 100%x -  135dip, 70dip )
   
   
    Private sld As StateListDrawable
    sld.Initialize
    Private c1,c2 As ColorDrawable
    c1.Initialize(0xfffe6d6a, 5dip )
    c2.Initialize(Colors.DarkGray, 5dip )
    sld.AddState(sld.State_Pressed, c2)
    sld.AddCatchAllState(c1)
   
    btn.Initialize("btnItemWithButton")
    btn.Text = btnName
    btn.TextSize = 13
    btn.TextColor = Colors.White
    btn.Background = sld
    pbase.AddView( btn, 100%x - 115dip, 15dip, 100dip, 40dip )
   
   
    Private divider As Panel
    divider.Initialize("")
    divider.Color = Colors.ARGB(30, 0,0,0)
    pbase.AddView( divider, 15dip, 70dip - 1, 100%x - 30dip, 1 )
    Return pbase
   
End Sub
 

sorex

Expert
Licensed User
Longtime User
you should store the value in the .tag property.

in that click event you can read out the tag of the sender again.

B4X:
clv.Add(addItemWithButton("click here","here","123"),"")
    clv.Add(addItemWithButton("click here","here","456"),"")
    clv.Add(addItemWithButton("click here","here","789"),"")


Sub btnItemWithButton_Click 'the buttom in custom listview
dim b as button=sender
log(b.tag)   
End Sub



Sub addItemWithButton ( itemname As String , btnName As String, btnValue as string ) As Panel
...
    btn.Initialize("btnItemWithButton")
    btn.Text = btnName
btn.tag=btnValue '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    btn.TextSize = 13
    btn.TextColor = Colors.White
    btn.Background = sld
    pbase.AddView( btn, 100%x - 115dip, 15dip, 100dip, 40dip )
...     
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Don't create the items layout programmatically. It will be easier to use the designer for this.

2. Never use Msgbox. Especially not for debugging. Use Log instead (or MsgboxAsync).

3. As demonstrated in the example: https://www.b4x.com/android/forum/t...-cross-platform-customlistview.84501/#content you should use GetItemFromView:
B4X:
Sub btnItemWithButton_Click
 Dim index As Int = clv.GetItemFromView(Sender)
 Dim value As String = clv.GetValue(index)
 
Upvote 0
Top