Android Question B4xtable Question [SOLVED]

ProjectGroup19

Active Member
Licensed User
Greeting Fam,
I want a create two different columns for a single b4xtable.
The Default columns is Election, Status then I load the data and after I click a button, the columns should now be Name, VoterId, Year, then I load the data.

Please, if it's possible, how do I do it??

Thank you.
 
Solution
Please, if it's possible
Yes, of course. Here is an example B4XPages project with a SQLite database db1.db with 3 columns in a table called student. The SQLite columns are: studnum, studname, studadd.. When the main page is loaded it shows 2 columns in B4XTable. Then, when you click a button the B4Xtable is loaded with all 3 columns. You can adapt it to your database or list or csv file, or whatever the source of your data is. It will do what you asked for.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private SQL As SQL
    Private Const Filename As String = "db1.db"
    Private xui As XUI
    Private B4XTable1 As B4XTable
End Sub

Public Sub Initialize
    If File.Exists(xui.DefaultFolder, Filename) = False Then...

Mahares

Expert
Licensed User
Longtime User
Please, if it's possible
Yes, of course. Here is an example B4XPages project with a SQLite database db1.db with 3 columns in a table called student. The SQLite columns are: studnum, studname, studadd.. When the main page is loaded it shows 2 columns in B4XTable. Then, when you click a button the B4Xtable is loaded with all 3 columns. You can adapt it to your database or list or csv file, or whatever the source of your data is. It will do what you asked for.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private SQL As SQL
    Private Const Filename As String = "db1.db"
    Private xui As XUI
    Private B4XTable1 As B4XTable
End Sub

Public Sub Initialize
    If File.Exists(xui.DefaultFolder, Filename) = False Then  'filename is database name
        File.Copy(File.DirAssets, Filename, xui.DefaultFolder, Filename)
    End If
    SQL.Initialize(xui.DefaultFolder, Filename, False)
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    B4XTable1.AddColumn("Student_Num", B4XTable1.COLUMN_TYPE_NUMBERS)
    B4XTable1.AddColumn("Student_Name", B4XTable1.COLUMN_TYPE_TEXT)
    
    Dim rs As ResultSet = SQL.ExecQuery("SELECT studnum, studname FROM student ")  
    Dim Data As List
    Data.Initialize
    Do While rs.NextRow
        Dim r(rs.ColumnCount) As String
        For j=0 To rs.ColumnCount -1
            r(j) = rs.getstring(rs.GetColumnName(j))
        Next
        Data.Add(r)
    Loop
    rs.Close    
    B4XTable1.SetData(Data)
End Sub

Private Sub Button1_Click
    B4XTable1.Clear
    B4XTable1.AddColumn("Student_Num",  B4XTable1.COLUMN_TYPE_NUMBERS)
    B4XTable1.AddColumn("Student_Name", B4XTable1.COLUMN_TYPE_TEXT)
    B4XTable1.AddColumn("Student_City", B4XTable1.COLUMN_TYPE_TEXT)
    
    Dim rs As ResultSet = SQL.ExecQuery("SELECT * FROM student ")
    Dim Data As List
    Data.Initialize
    Do While rs.NextRow
        Dim r(rs.ColumnCount) As String
        For j=0 To rs.ColumnCount -1
            r(j) = rs.getstring(rs.GetColumnName(j))
        Next
        Data.Add(r)
    Loop
    rs.Close
    B4XTable1.SetData(Data)
End Sub
 
Upvote 0
Solution

ProjectGroup19

Active Member
Licensed User
Yes, of course. Here is an example B4XPages project with a SQLite database db1.db with 3 columns in a table called student. The SQLite columns are: studnum, studname, studadd.. When the main page is loaded it shows 2 columns in B4XTable. Then, when you click a button the B4Xtable is loaded with all 3 columns. You can adapt it to your database or list or csv file, or whatever the source of your data is. It will do what you asked for.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private SQL As SQL
    Private Const Filename As String = "db1.db"
    Private xui As XUI
    Private B4XTable1 As B4XTable
End Sub

Public Sub Initialize
    If File.Exists(xui.DefaultFolder, Filename) = False Then  'filename is database name
        File.Copy(File.DirAssets, Filename, xui.DefaultFolder, Filename)
    End If
    SQL.Initialize(xui.DefaultFolder, Filename, False)
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    B4XTable1.AddColumn("Student_Num", B4XTable1.COLUMN_TYPE_NUMBERS)
    B4XTable1.AddColumn("Student_Name", B4XTable1.COLUMN_TYPE_TEXT)
   
    Dim rs As ResultSet = SQL.ExecQuery("SELECT studnum, studname FROM student ") 
    Dim Data As List
    Data.Initialize
    Do While rs.NextRow
        Dim r(rs.ColumnCount) As String
        For j=0 To rs.ColumnCount -1
            r(j) = rs.getstring(rs.GetColumnName(j))
        Next
        Data.Add(r)
    Loop
    rs.Close   
    B4XTable1.SetData(Data)
End Sub

Private Sub Button1_Click
    B4XTable1.Clear
    B4XTable1.AddColumn("Student_Num",  B4XTable1.COLUMN_TYPE_NUMBERS)
    B4XTable1.AddColumn("Student_Name", B4XTable1.COLUMN_TYPE_TEXT)
    B4XTable1.AddColumn("Student_City", B4XTable1.COLUMN_TYPE_TEXT)
   
    Dim rs As ResultSet = SQL.ExecQuery("SELECT * FROM student ")
    Dim Data As List
    Data.Initialize
    Do While rs.NextRow
        Dim r(rs.ColumnCount) As String
        For j=0 To rs.ColumnCount -1
            r(j) = rs.getstring(rs.GetColumnName(j))
        Next
        Data.Add(r)
    Loop
    rs.Close
    B4XTable1.SetData(Data)
End Sub
Thank you @Mahares
 
Upvote 0
Top