B4J Question [B4J] How to take the right Tableview Selected Cell value

NikB4x

Member
Licensed User
Longtime User
Hi All,
I'm using a simple Tableview and I need to take the selected cell value. I used this code and it works perfect:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TableView1 As TableView
    Private Label1 As Label
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    filltable
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

Sub filltable
    TableView1.SingleCellSelection=True
    TableView1.SetColumns(Array As String("Col1","Col2","Col3","Col4"))
    For i=1 To 100
        Dim row() As Object = Array As Object("Value C1: " & (i+10) , "Value C2: " & (i+20), "Value C3: " & (i+30),"Value C4: " & (i+40))
        TableView1.Items.Add(row)
    Next
End Sub

Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
    Label1.Text=Cell
End Sub

But if I change the layout of the table dragging, for example, the last column in the first position at runtime, the label shows the wrong value if I click f.e. the first item.
Is there a way to avoid this problem?
Thanks!
 

Attachments

  • test.zip
    2.3 KB · Views: 268

Daestrum

Expert
Licensed User
Longtime User
Quick fix using JavaObject library
B4X:
Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
' get real order of columns
 Dim cols As JavaObject = asJO(TableView1).RunMethod("getColumns",Null)
' read column ColIndex for cell at RowIndex 
Label1.Text = cols.RunMethodjo("get",Array(ColIndex)).RunMethod("getCellData",Array(RowIndex))
End Sub

Sub asJO(o As JavaObject) As JavaObject
 Return o
End Sub
 
Upvote 0

NikB4x

Member
Licensed User
Longtime User
I'm still working with this issue...

Daestrum sent me a quick fix, but I notice that this fix doesn't work always!
It's really strange because sometimes it works and sometimes it doesn't, apparently without a valid reason.
When it doesn't work, the runtime error is this:


I attached the sample source.
Hope someone can explain why... Thanks
 

Attachments

  • sample.zip
    2.4 KB · Views: 244
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The only way I could 'break' your example and get a similar error was to put an array of items into a cell in a column.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…