B4J Question Tableview RowSelected using controls in Cells

Teech

Member
Licensed User
Longtime User
I have populate a Tableview using a Wrapper, where some controls are 'Label' and others are 'TextField'
B4X:
Private Sub DrawTablaView
    'Columns Tableview
    Dim columns() As String=Array As String("Code","Description")
    Dim columnsSize As Map
    columnsSize.Initialize
    columnsSize.Put(0,100) 'Code
    columnsSize.Put(1,tv.PrefWidth-100) 'Description
       
    'Draw Columns
    tv.Items.Clear
    tv.SetColumns(columns)
    For Each col In columnsSize.Keys
        tv.SetColumnSortable(col,False)
        tv.SetColumnWidth(col,columnsSize.Get(col))
    Next
    For Each c As Component In mList
        Dim row(columns.Length) As Object
        RowBuilder(row,c)
        tv.Items.Add(row)
    Next
End Sub
Private Sub RowBuilder(row() As Object, c As Component)
    For i=0 To row.Length-1
        Select Case i
            Case 0 'Code
                Dim lb As Label
                lb.Initialize("lbCode")
                lb.Text=NumberFormat(c.Code,1,0)
                lb.Tag=c
                row(i)=WrapControl(lb)
            Case 1
                Dim tf As TextField
                tf.Initialize("tfDescription")
                tf.Text=c.Description
                row(i)=WrapControl(tf)
        End Select
    Next
End Sub
Sub WrapControl(control As Object) As Pane
    Dim pn As AnchorPane
    pn.Initialize("")
    pn.AddNode(control, 0, 0, -1, -1)
    pn.FillHorizontally(control, 0, 0)
    pn.FillVertically(control,0,0)
    Return pn
End Sub
When I click on a cell containing a Label the RowSelected property is changed, the backcolor of the row is changed and all the other tableview properties are well changed.
When I click on a Cell containing a Textfield this one get the focus but there aren't any effects on the Tableview. I'd like that tableview is affected as if I click on a label.

Have you got any idea how can I fix this issue? I Attach an example.
Many Thanks.
 

Attachments

  • RowSelected.zip
    32.2 KB · Views: 272

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code to select the row when the TextField is focused:
B4X:
    For Each c As Component In mList
       Dim row(columns.Length) As Object
       RowBuilder(row,c, tv.Items.Size)
       tv.Items.Add(row)
   Next
End Sub

Private Sub RowBuilder(row() As Object, c As Component, RowIndex As Int)
   For i=0 To row.Length-1
       Select Case i
           Case 0 'Code
               Dim lb As Label
               lb.Initialize("lbCode")
               lb.Text=NumberFormat(c.Code,1,0)
               lb.Tag=c
               row(i)=WrapControl(lb)
           Case 1
               Dim tf As TextField
               tf.Initialize("tfDescription")
               tf.Text=c.Description
               row(i)=WrapControl(tf)
               tf.Tag = RowIndex
       End Select
   Next
End Sub

Sub tfDescription_FocusChanged (HasFocus As Boolean)
   If HasFocus Then
       Dim tf As TextField = Sender
       tv.SelectedRow = tf.Tag
   End If
End Sub

The row color will be grey instead of blue because the TableView is not focused. You can change the colors with a CSS file.
 
Upvote 0

Teech

Member
Licensed User
Longtime User
Thank you, very useful.
Based on what you have suggested to me I thought to use a Map ('mRows') to save rows, where the Key is a Component Object and related Value is the row() added to the TableView.
So, code to select row is:
B4X:
Private Sub tfDescription_FocusChanged (HasFocus As Boolean)
    If HasFocus Then
        Dim tf As TextField=Sender
        tv.SelectedRow=RowIndexFromComponent(tf.tag)
    End If
End Sub
Private Sub RowIndexFromComponent(c As Component) As Int
    Dim ret As Int=-1
    ret=tv.Items.IndexOf(mRows.Get(c))
    Return ret
End Sub
I attach an example.
 

Attachments

  • RowSelected.zip
    34.9 KB · Views: 276
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…