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

Discussion in 'Android Questions' started by Sai Main Seng Kham, Jul 14, 2019 at 5:25 PM.

  1. Sai Main Seng Kham

    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


    Code:
    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, 00100%x100%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, 
    15dip0100%x -  135dip70dip )
       
       
        
    Private sld As StateListDrawable
        sld.Initialize
        
    Private c1,c2 As ColorDrawable
        c1.Initialize(
    0xfffe6d6a5dip )
        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 - 115dip15dip100dip40dip )
       
       
        
    Private divider As Panel
        divider.Initialize(
    "")
        divider.Color = 
    Colors.ARGB(300,0,0)
        pbase.AddView( divider, 
    15dip70dip - 1100%x - 30dip1 )
        
    Return pbase
       
    End Sub
     
  2. sorex

    sorex Expert Licensed User

    you should store the value in the .tag property.

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

    Code:
    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 - 115dip15dip100dip40dip )
    ...     
    End Sub
     
    Last edited: Jul 14, 2019 at 7:35 PM
    microbox likes this.
  3. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    Sub btnItemWithButton_Click
     
    Dim index As Int = clv.GetItemFromView(Sender)
     
    Dim value As String = clv.GetValue(index)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice