B4J Question How to get tableview row index based on button clicked inside the row?

Mashiane

Expert
Licensed User
Longtime User
Hi there

I have two buttons inside a pane and this pane is at row(x) object in a tablewview row. I can have numerous rows in my tableview. What I would like to do is when the button is clicked on any row, get the row index number the button is on.

I believe the row index changes when listview items change thus when adding a row I cannot tag its position to the button.

How can I get the index of the row my button click happened on?

Thanks
 

Daestrum

Expert
Licensed User
Longtime User
Simple example (you may need to allow for extra parent call as your button is in a pane)
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim tv As TableView
End Sub
' uses JavaObject
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    tv.Initialize("tv")
    tv.SetColumns(Array("one","two","three"))
    tv.SetColumnWidth(0,100)
    tv.SetColumnWidth(1,100)
    tv.SetColumnWidth(2,100)
    MainForm.RootPane.AddNode(tv,0,0,310,500)
    For a = 0 To 10
        Dim b As Button
        b.Initialize("bu")
        b.Text = "row"&a
        tv.Items.Add(Array(a,"text"&a,b))
    Next
End Sub
Sub bu_Click
    ' first parent = column , second = row , index = row index
    Log(asJO(Sender).RunMethodJO("getParent",Null).RunMethodjo("getParent",Null).RunMethod("getIndex",Null))   
End Sub
Sub asJO(o As JavaObject)As JavaObject
    Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 
Upvote 0
Top