B4J Question TableView: Prevent first cell to be selected when updating row

KMatle

Expert
Licensed User
Longtime User
I change single cells with this code to a textfield (to let the user edit it inside the table). This works like a charm

B4X:
Sub DevicesTV_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)

Log("Actual Row: " & RowIndex & " Column: " & ColIndex)

If ColIndex=0 Then Return

Dim actualrow(DevicesTV.ColumnsCount) As Object = DevicesTV.Items.Get(RowIndex)
Dim tf As TextField
tf.Initialize("")
tf.Text=Cell
actualrow(ColIndex)=tf
Log("Changed: " & ColIndex)
actualrow(ColIndex)=tf
DevicesTV.Items.Set(RowIndex,actualrow)

End Sub

Problem here is that there are two events fired:

B4X:
Actual Row: 0 Column: 5
Changed: 5
Actual Row: 0 Column: 0

First the actual clicked cell and as a second one row 0 cell 0.

It seems that the first cell is always selected automatically when you update the row. This causes the 2nd event to be fired. As you can click on that cell, you can't check it by code if it is really clicked (except you filter it as first and second event). Is there a way to prevent this behaviour? I did not find a setting.
 

amykonio

Active Member
Licensed User
Longtime User
Is there a reason you don't make that column a textfield from the start (when loading data to your table)?
Why you want to modify it in SelectedCellChanged event?
If this column was a textfield from the beginning you wouldn't need to handle that event.
I think.
 
Upvote 0

amykonio

Active Member
Licensed User
Longtime User
Looks better and the handling feels damned good :D By the way: This doesn't really come close to my question.
Yes. You are right. It looks better.
But the way you try to do it makes it complex...
Anyway.
I added a map (Global):

B4X:
Private mapEditedColumns As Map

And made some modifications to your code:

B4X:
Sub DevicesTV_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
    If Not(mapEditedColumns.IsInitialized) Then mapEditedColumns.Initialize
    
    If mapEditedColumns.GetDefault(RowIndex & "_" & ColIndex, 0) = 0 Then
        Log("Not Edited")
        mapEditedColumns.Put(RowIndex & "_" & ColIndex, 1)
        Log("Actual Row: " & ColIndex & " Column: " & ColIndex)
    
        If ColIndex=0 Then Return
    
        Dim actualrow(DevicesTV.ColumnsCount) As Object = DevicesTV.Items.Get(RowIndex)
        Dim tf As TextField
        tf.Initialize("")
        tf.Text=Cell
        actualrow(ColIndex)=tf
        Log("Changed: " & ColIndex)
        actualrow(ColIndex)=tf
        DevicesTV.SingleCellSelection = False
        DevicesTV.Items.Set(RowIndex,actualrow)
        DevicesTV.SingleCellSelection = True
        DevicesTV.SelectCell(RowIndex,ColIndex)
    Else
        Log("Edited")
    End If
End Sub

It must be near to what I understand you want to do.
But the map mast be initialized when the table is repopulated.
Also if you insert, delete rows you have to make changes to the map.
So it can become complex.
Also, that way I avoid to re-change cells that already contain textfields.
 
Upvote 0
Top