Wish B4X Table RowID begins with 1 and not with 0 why

klaus

Expert
Licensed User
Longtime User
Hi Erel,
I have begun to use the B4A Table CustomView.
In the B4XTable1.CellClicked (ColumnId As String, RowId As Long) routine the RowID begins with 1, why?
We are all used that indexes begin with 0, why does it begin here with 1?
Trying
B4X:
mp = bxtTable.GetRow()
Log(mp.Get(ColumnId))
Returns null.
I put Wish as the prefix, because I would like to have this index coherent with all the others.
 

klaus

Expert
Licensed User
Longtime User
... it shouldn't return the RowID 0.
No, it doesn't return 0.
It returns the correct value!
I use it with a SQLite database and I memorize the rowids of the database rows in a List, there the first index is 0.
To read in the database the selected row i need to use CurrentIndex = RowId - 1.
 

LucaMs

Expert
Licensed User
Longtime User
Usually, also an autoincremented field index starts with 1 (I'm not sure but you cannot start with 0, there).


But you are referring to the rowID of the table that I never use (or rarely, just for rare specific reasons).
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I've noticed that you're somewhat reluctant to give me a "Like", Klaus.
Who knows, maybe every now and then, among the many nonsense I write, sometimes an exact thing will happen 😄.

It is not important, of course: I am unable to eat them 😄
 

Mahares

Expert
Licensed User
Longtime User
Look at the code example in my post!
LucaMS is correct. If I click on the 10th row of the data here is what I get:
B4X:
 Log($"rowId: ${RowId} "$  )   '10 which is row # clicked. Note that header cell is 0, then cell 1 then 2, etc.   
   Dim mp As Map
    mp.Initialize
    mp = B4XTable1.GetRow(RowId)
    Log(mp)   'shows: col name and  the number 10 if I clicked the 10th row excluding the header row
My stored data column name is: Entry and the cell content for the 10th row is 10, since my data starts with 1, then 2, then 3, etc.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two things worth clearing:

1. The RowId is a random unique value. Don't assume that it starts from 1. The only thing that you can assume is that RowId = 0 means an empty row. I use it all the time in the DataUpdated event.

If you want to get the visible row number then you must use B4XTable.VisibleRowIds.

2. You need to add 1 when you want to get the "CellLayout" of a visible row from Column.CellLayouts. It happens because the first layout is the header layout.

Example that marks rows where the value of a column is larger than 10:

java_4c1NGO836B.png


B4X:
Sub B4XTable1_DataUpdated
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        If RowId > 0 Then 'non-empty row
            Dim Value As Double = B4XTable1.GetRow(RowId).Get(numberColumn.Id)
            If Value > 10 Then
                SetRowColor(i, xui.Color_LightGray)
                Continue '<----
            End If
        End If
        SetRowColor(i, xui.Color_White)
    Next
End Sub

Sub SetRowColor (RowIndex As Int, Clr As Int)
    For Each c As B4XTableColumn In B4XTable1.VisibleColumns
        Dim pnl As B4XView = c.CellsLayouts.Get(RowIndex + 1) '+1 because of the header
        pnl.Color = Clr
    Next
End Sub
 

klaus

Expert
Licensed User
Longtime User
Don't worry, in my real code I use only the RowID and it works.
I simply supposed that ColumnId and RowID were the indexes of the column and the row and not IDs with a different meaning.
The reason of this thread was the wish to be coherent with all B4X objects having indexes beginning with 0.
 

Mahares

Expert
Licensed User
Longtime User
ColumnId As String
Calling a column name or column header ColumnID is also a bit confusing because you always think of ID as a number when it comes to database. It should have been named: ColumnHeader or ColumnName
The same here: B4XTable1.GetRow(RowId).Get(numberColumn.Id) Id is normally reserved for a number but here it is the column content. It should have been named: ColumnContent or ColumnValue
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Calling a column name or column header ColumnID is also a bit confusing because you always think of ID as a number when it comes to database. It should have been named: ColumnHeader or ColumnName
The same here: B4XTable1.GetRow(RowId).Get(numberColumn.Id) Id is normally reserved for a number but here it is the column content. It should have been named: ColumnContent or ColumnValue
1. It has nothing to do with this thread.

2. Don't confuse ID with an index. ID doesn't need to be a number. There is an important distinction between the column title and its id. In most cases they are the same but they don't need to be the same. The ID is an internal field that the user never sees.
 
Top