Android Question B4XTable

Johan Schoeman

Expert
Licensed User
Longtime User
How do one cycle through all rows in a B4XTable? This does not work as it seems to only rely on the size of the "visible table" and not the complete table (visible and not visible)

B4X:
    For i = 1 To B4XTable1.Size - 2
        Dim m As Map = B4XTable1.GetRow(i)

It gives this error:
B4X:
java.lang.IndexOutOfBoundsException: Index: 11, Size: 11

....that seems to be related to this line of code:

B4X:
B4XTable1.MaximumRowsPerPage = 10

I have searched the forum but don't get a solution to the error. Anyone that can shed some light on this problem?

Thanks

JS
 

elsanto

Member
Licensed User
Longtime User
I am working with B4xTable and it is a solution to work with the internal sqlite that it generates, and directly working with it is very easy
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I am trying to "log" the data of each row with....

B4X:
Sub Button1_Click
    

    For Each column As B4XTableColumn In B4XTable1.columns
        Log(column.Id)
        
    Next
    
    Dim numOfPages As Int = B4XTable1.Size / B4XTable1.RowsPerPage
    If B4XTable1.Size Mod B4XTable1.RowsPerPage > 0 Then
        numOfPages = numOfPages + 1
    End If
    Log("numOfPages = " & numOfPages)
    
    
    For j = 1 To numOfPages
        For i = 1 To B4XTable1.RowsPerPage
            Dim m As Map = B4XTable1.GetRow(i)
            Dim pnl As B4XView = FlagsColumn.CellsLayouts.Get(i) '+1 because the first cell is the header
            Dim iv As B4XView = pnl.GetView(1) 'ImageView will be the 2nd child of the panel. The built-in label is the first.
            Dim mbm As Bitmap = iv.GetBitmap
            ImageView1.Bitmap = mbm
            Log("m = " & m.Get("Flags"))
            Log("m = " & m.Get("Id"))
            Log("m = " & m.Get("Name"))
            Log("m = " & m.Get("Alpha2"))
            Log("m = " & m.Get("Alpha3"))
            Log("table size = " & B4XTable1.Size)
        Next
        B4XTable1.CurrentPage = j                'change the page to the next page
        
    Next
    
End Sub

......but it just logs the data of the first 10 rows in the table (B4XTable1.MaximumRowsPerPage = 10)

B4XTable1.CurrentPage = j -> it does not get the new page data in the above code. Any other way to do it other than having do dig into SQLite?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No need to "dig into SQLite". You do need to get the data, without digging, from the database:
B4X:
Dim o() As Object = B4XTable1.BuildQuery(False)
Dim rs As ResultSet = B4XTable1.sql1.ExecQuery2(o(0), o(1))
Do While rs.NextRow
    Dim Name As String = rs.GetString(NameColumn.SQLID)
    Log(Name)
Loop
rs.Close
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another option if you prefer working with Table.GetRow:

B4X:
For Each rowid As Long In GetRowIds
    Dim m As Map = B4XTable1.GetRow(rowid)
    Log(m)
Next

GetRowIds:
B4X:
Private Sub GetRowIds As List
    Dim res As List
    res.Initialize
    Dim rs As ResultSet = B4XTable1.sql1.ExecQuery2("SELECT rowid FROM data", Null)
    Do While rs.NextRow
        res.Add(rs.GetLong("rowid"))
    Loop
    rs.Close
    Return res
End Sub

If the data set is large then the previous code will probably be faster.
 
Upvote 0
Top