B4A Library [Class] Flexible Table

LucaMs

Expert
Licensed User
Longtime User
Hi, Klaus.

Since in the Italian forum someone asked how to use the "Flexible Table" along with the RDC, I added a routine for this purpose: LoadRDCResult(Result As DBResult, AutomaticWidths As Boolean).

It is virtually identical to LoadSQLiteDB; In fact, you could groped to optimize the code between the two routines, but I did not have the patience.

If you like it or want to create a similar but better ...

Thanks

B4X:
'load data from a RDC Request
'Result = DBResult object got from a RDC request
'AutomaticWidths  True > set the column widths automaticaly
Public Sub LoadRDCResult(Result As DBResult, AutomaticWidths As Boolean)
               
    cAutomaticWidths = AutomaticWidths
    NumberOfColumns = Result.Columns.Size
    innerClearAll(NumberOfColumns)
   
    Dim Headers(NumberOfColumns) As String
    Dim ColumnWidths(NumberOfColumns) As Int
    Dim HeaderWidths(NumberOfColumns) As Int
    Dim DataWidths(NumberOfColumns) As Int
    Dim col, row As Int
    Dim str As String
    For col = 0 To NumberOfColumns - 1
        Headers(col) = Result.Columns.GetKeyAt(col)
        If AutomaticWidths = False Then
            ColumnWidths(col) = 130dip
            HeaderWidths(col) = 130dip
            DataWidths(col) = 130dip
        Else
            HeaderWidths(col) = cvs.MeasureStringWidth(Headers(col), Typeface.DEFAULT, cTextSize) + 8dip + cLineWidth
            DataWidths(col) = 0
           
            Dim FieldValue As Object
            For row = 0 To Result.Rows.Size - 1
                Dim Record() As Object = Result.Rows.Get(row)
                FieldValue = Record(col)
                If GetType(FieldValue) = "java.lang.String" Then
                    DataWidths(col) = Max(DataWidths(col), cvs.MeasureStringWidth(str, Typeface.DEFAULT, cTextSize) + 8dip + cLineWidth)
                End If
            Next
            ColumnWidths(col) = Max(HeaderWidths(col), DataWidths(col))
        End If
    Next
    SetHeader(Headers)
    SetColumnsWidths(ColumnWidths)
   
    For Each Record() As Object In Result.Rows
        Dim R(NumberOfColumns) As String
        Dim FieldV As String
        For col = 0 To NumberOfColumns - 1
            FieldV = Record(col)
            R(col) = FieldV
        Next
        AddRow(R)
    Next

End Sub
 

klaus

Expert
Licensed User
Longtime User
Hi Luca
I looked to add your routine into the Table class.
Unfortunately there is a problem because the routine uses the specific DBResult Type in the Sub declaration:
Public Sub LoadRDCResult(Result As DBResult, AutomaticWidths As Boolean)

- For users not using the LoadRDCResult routine would need the Type declaration in Class_Globals
Type DBResult (Tag As Object, Columns As Map, Rows As List)
Without it, an error is raised.

- But the users using the LoadRDCResult routine already have the Type declaration somewhere else and will get an error key aleady exists.

So I don't want add the routine into the Class but copy your code in the first post.
 

LorenzoTRANSFEREX

Member
Licensed User
Longtime User
Hi, how can I remove all rows from a data table?
I get an error java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 with this:

For i=Table1.Size-1 To 0 Step -1
Table1.RemoveRow(i)
Next

Thanks and congratulations for this library.
 

itgirl

Active Member
Licensed User
Longtime User
Hello, let's say i have 2 columns one for names and the other for grades , how can i sort the table by the grades from the Highest to the Lowest ?

I tried all kind of sorting but seems not working (asc - desc ) only
 

Reviewnow

Active Member
Licensed User
Longtime User
Select * from mytable order by grades desc
 

itgirl

Active Member
Licensed User
Longtime User
@klaus
Yeah i already tried this as well, but the column has numbers


100
101
1


and the sorting ( Table1.sortTable(ColumnNumber, False) ) or ( Table1.sortTable(ColumnNumber, True) ) return the numbers sorted as strings not as numbers from highest to lowest ...


i mean if the Grades column has

2323
22
333

after sorting i get

22
2323
333

but i need


2323
333
22
 

Mahares

Expert
Licensed User
Longtime User
To sort by a column that is a string and has all numbers, you can use the below SQLite statement. It will sort the grades for you in the proper order:
B4X:
txt = "SELECT CAST(GRADE AS INTEGER) AS MYGRADE, NAME FROM tblGrades ORDER BY MYGRADE DESC"  
Cursor1=SQL1.ExecQuery(txt)
 

itgirl

Active Member
Licensed User
Longtime User
@ Mahares

Thank you for your reply but again that would be useful if i was using sql which im not im using a simple loadcsv into table
 

LucaMs

Expert
Licensed User
Longtime User
Can you modify your csv file? IF you can set:
" 22"
" 333"
"2323"

(same lenght)

then you've solved


[UPDATED] Most likely it will be enough to right-align the column using SetCellAlignments
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Try the attached project.
I added Sub sortTableNum(col As Int, dir As Boolean) a routine allowing to sort numeric values.
It includes a small csv file with a Grades column.
 

Attachments

  • TableV1_32.zip
    42 KB · Views: 223

LucaMs

Expert
Licensed User
Longtime User
I thought there was a way to align the data in a single column; I looked at the code and it seems to me that it is possible for all columns or a single cell.

It would be useful, in that case.

[but it would be necessary to fill the values with a few spaces left]
 
Last edited:

itgirl

Active Member
Licensed User
Longtime User
@klaus

OMG ur amazing with ur Libs, Examples and Codes. your example was exactly what i was talking about , thank you very much it was a great help and i learned alot from ur simple yet amazing code i think it should be included in the class coz it helps alot , thank you Kalus
 

LucaMs

Expert
Licensed User
Longtime User
Klaus,

given that in the future I'll want to add some function to your class , I allowed myself to organize it using the Regions (as I understand it better).

Use ALT + E - O - C and see everything in blocks is very useful.
 

Attachments

  • Table.zip
    11.1 KB · Views: 235

klaus

Expert
Licensed User
Longtime User
@LucaMs
This routine
'sets the cell alignment for each column each column can have a diferent alignment
'Example:'<code>'Dim alignments() As Int
'alignments = Array As Int (Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL), Gravity.CENTER, Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL), Gravity.CENTER)
'Table1.CellAlignments(alignments)</code>
Public Sub SetCellAlignments(Alignments() As Int)
allows to set the gravity of each column.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…