I have a B4XTable with 3 columns using an array of columns like this:
B4X:
ColNames =Array As String("ID","Name", "Street")
For i= 0 To ColNames.Length -1
If i=0 Then
B4XCol(i) = B4XTable1.AddColumn(ColNames(i), B4XTable1.COLUMN_TYPE_NUMBERS)
Else
B4XCol(i) = B4XTable1.AddColumn(ColNames(i), B4XTable1.COLUMN_TYPE_TEXT)
End If
B4XCol(i).Width=ColWidthOriginal(i)
Next
Later I use QueryBuilder to populate the table with only 2 of the 3 columns. I can add the 2 columns B4XCol(1) and B4XCol(2) like this:
Is there a way to set up the columns using the corresponding array elements B4XCol(1) and B4XCol(2) instead of rewriting the entire 2 lines above
Thank you
It is a bit difficult to give a concrete answer as I'm not sure what you are doing with B4XCol. I have a feeling that you should make it a List instead of an array. This will allow you to remove the first item from the list.
We are not talikng about hiding columns. I reuse the same B4XTable layout by removing the views and populating the table with a different set of columns. Instead of using something like this in the code:
Added this to the post: If I use this it works, but I was hoping for the above code to work:
B4X:
B4XTable1.AddColumn(B4XColumns.Get(1), 1) 'B4XColumns is a list of the columns names
B4XTable1.AddColumn(B4XColumns.Get(2), 1)
B4XTable1.SetData(l)
Added this later: I can use this also. It works:
B4X:
Type MyTableCols( column As String, coltype As Int) 'in globals
Private MyList As List[ 'in globals
B4X:
Root.RemoveAllViews
Root.LoadLayout("2")
Dim ct As MyTableCols =MyList.Get(1)
B4XTable1.AddColumn(ct.column, ct.coltype) 'col name, col type
Dim ct As MyTableCols =MyList.Get(2) 'MyList is a list of custom Type
B4XTable1.AddColumn(ct.column, ct.coltype)
B4XTable1.SetData(l)
I think the best solution is to do this. I will stick with it, unless someone has a better approach for what I was trying to achieve:
B4X:
Root.RemoveAllViews
Root.LoadLayout("2") 'no need to use layout 3
Dim c As B4XTableColumn=B4XCol(1)
B4XTable1.AddColumn(c.Title, c.ColumnType) 'col name, col type
Dim c As B4XTableColumn=B4XCol(2)
B4XTable1.AddColumn(c.Title, c.ColumnType) 'col name, col type
B4XTable1.SetData(l)