B4J Question tableView, setting column widths, SQL query

Avon

Member
Licensed User
Longtime User
By embedding column metadata in the column alias of a query, I can retreive the column title, alignment, width and font at run time... but that is in a different world!

Could I do this with B4J? then I could reuse the same code for every tableview...

SELECT lastname || ',' || firstname "<CS Name A=L, W=140, F=3>",
subs "<CS Subscription A=R, W=40, F=3>",
renewdate "<CS Due_Date A=C, W=70, F=3>"
FROM PEOPLE
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
If i understood your inquery right.

No you cannot add metadata to a query.

But you can resize your tableview columns accordingly to its width with this event and function:
B4X:
private Sub Tw_Resize (Width As Double, Height As Double)
    Tw.SetColumnWidth(1, 20)
    Tw.SetColumnWidth(2, 20)
    Tw.SetColumnWidth(3, 30)
    Tw.SetColumnWidth(4, 20)
    Tw.SetColumnWidth(5, 20)
End Sub
 
Upvote 0

Avon

Member
Licensed User
Longtime User
Enrique, I think ExecuteMap from DBUtils will get the column aliases. Then I need to set the column widths as per your example, also specify the alignment and font. But the column names will be the alias strings, so I will have to change these too, InsertMaps maybe?

Still looking for column alignment and font selection methods.
 
Upvote 0

EnriqueGonzalez

Expert
Licensed User
Longtime User
Sorry.

for specifically the name of the table you can use this function inside DBUTILS

This will asign the name of the columns of the query into the tableview.
B4X:
Public Sub ExecuteTableView(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then 
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As String
        For col = 0 To cur.ColumnCount - 1
            values(col) = cur.GetString2(col)
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

For the alignment of the HEADERS and font. its quite difficult, let me try to remember how to do it
 
Upvote 0

Avon

Member
Licensed User
Longtime User
Right, but if you use the query in post#1, the first column title will be <CS Name A=L, W=140, F=3>, the first field inside the <CS> is the column name, which has to be reloaded into the tableview as the column header, without having to hit the DB again.

The alignment is for the whole column, not just the column headers, BTW.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…