B4A Library [Class] Flexible Table

This thread will be used by Erel, Melamoud and myself to discuss / post new releases of the Table class.

The table class is a flexible UI component that enable scrollable table like UI, with sortable columns, multiselect rows etc

the table is very efficient, maintaining labels only for visible rows

old thread with details : http://www.b4x.com/forum/additional...view-supports-tables-any-size.html#post110901

The class depend on following libraries:
- StringUtils (standard)
- SQL (standard)
- JavaObject (standard)
- B4XCollections (standard)
- ScrollView2D (additional)

List of major features.
1. scrollable table UI
2. sortable columns
3. select a row, cell or multi select rows
4. callback for selection / click a cell / row
5. callback for long click action
6. read / write to CSV file

Current version --> 3.35 Custom View
Current version --> 1.44 Class

Other complementary routines:

Load data with the Remote Database Connector.


EDIT: LucaMs has written a routine to fill a table with a Remote Database Connector query result see post 182.
The routine hasn't been added into the Class for the reasons explained in post 183.
A sample program can be found HERE.

Code:
B4X:
'load data from a RDC Request
'Result = DBResult object got from a RDC request
'AutomaticWidths  True > set the column widths automaticaly
'Written by LucasMs
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

This is another routine updated by cimperia in post #392 using a Map for the columns and a List for the rows.
B4X:
'load data from a RDC Request
'A RDC request returns a DBResult object, therefore this method
'could be called as is:
'LoadRDCResult(DBResult.Columns, DBResult.Rows, True)
'AutomaticWidths  True > set the column widths automaticaly
'Written by LucasMs
Public Sub LoadRDCResult(Columns As Map, Rows As List, AutomaticWidths As Boolean)
  cAutomaticWidths = AutomaticWidths
  NumberOfColumns = 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) = 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 Rows.Size - 1
        Dim Record() As Object = 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 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


Load data from a MSMariaDB database.

Another routine for loading data from a MSMariaDB database can be found in post#727.
Thanks to @Magma.

Updates:
EDIT: 2024.04.21 Version 3.35
Replaced the original sorting functions by new ones kindly provided by forum member RB Smissaert.
Amended first scroll scrolling back to 0
Amended Header with transparent color

EDIT: 2024.01.13 Version 3.33
Changed possible values for DataType
TEXT and NUMBER become T, R and I
Amended problem with column colors
Amended problems with SetHeaderColors and SetHeaderTextColors

Version 3.32
Amended Header and HeaderFirst problem in SaveCSVFromTable
Moved If (lblStatusline... from AddRow to ShowRow

Version 3.31
Added SingleLine property for the Designer
Added StatusLineHeight as a property
Added FastScrollLabelMaxChars as a property

EDIT: 2021.06.28 Version 3.30
Added a check for none numeric values for numeric sorting.

EDIT: 2021.06.28 Version 3.29
Amended problem with column colors
Version 3.28
Added NumberOfColumns in the code
Added TopRowIndex method
Version 3.27
Amended MultiSelect EDIT: 2020.09.02 Version 3.26
Amended problem with sort with remove accents
Amended problem with SetRowColorN
Added SetCellAlignmentColN method
Added SetHeaderAlignmentColN method

EDIT: 2020.08.05 Version 3.24
Amended problem with JumpToRowAndSelect not being selected.
Amended error when setting RowHeight before the table initialized

EDIT: 2020.06.19 Version 3.22
Amended error in the insertRowAt routine.

EDIT: 2020.05.25 Version 3.21
Amended bug with TextSize in fixed columns

EDIT: 2020.05.16 Version 3.20
Added fast scroll feature
Version 3.19
Improved automatic width calculation and hidden columns
Version 3.18
Added a check in RemoveRowColorN to ensure that Row is not out of bounds
Added ShowRow event
Amended automatic width calculations
Amended hidden column width problem

EDIT: 2020.04.21 Version 3.17
Amended HeaderHight problem with fixed columns

EDIT: 2020.04.21 Version 3.16
Amended two errors.

EDIT: 2020.04.14 Version 3.14
Added the methods below
- LoadSQLiteDB4(SQLite As SQL, Query As String, AutomaticWidths As Boolean)
loads SQLite data with data type checking
- LoadSQLiteDB5(SQLite As SQL, Query As String, Values() As String, AutomaticWidths As Boolean).
loads SQLite data with data type checking , similar to LoadSQLiteDB4 but for parametrized queries.
- GetColumnDataTypes As String(), returns an Array with the data type for each column.
- GetColumnDataType(Column As Int) As String, returns the data type of the fiven column.
Added the InnerTotalWidth property, read only.
Added multiple first fiexed columns
Added line colors

EDIT: 2020.03.10 Version 3.10
Amended bug reported HERE

EDIT: 2020.03.06 Version 3.09
Amended bug reported HERE.

EDIT: 2020.02.29 Version 3.08
Amended SetHeaderTypefaces method problem reprted HERE.
Added HeaderTypeface property.

EDIT: 2020.01.08 Version 3.07
Amended bug ShowStatisLine = False property bug.
Added MultiSelect property to Designer properties.
You need to open and close the Designer when you use the new version the first time to make the MultiSelect property active.

EDIT: 2019.12.28 Version 3.06
Amened some bugs

EDIT: 2019.12.25 Version 3.05
Added FirstColumnFixed property which allows to fix the first column.
Attention: You need to open and close the Designer to make the new property active.

EDIT: 2019.11.15 Version 3.04
- Added SelectedRowTextColor and SlectedCellTextColor properties
- Added ZeroSelections property, True > when a selected row is pressed it will be unselected False > it remains selected.

EDIT: 2019.11.12 Version 3.03
- Changed JumpToRowAndSelect(Row As Int, Col As Int) to JumpToRowAndSelect(Col As Int, Row As Int)
- Changed LoadSQLiteDB2 signature. Replaced the possible values from "T", "I", "R" to "TEXT", "NUMBER" for coherence with SetColumnDataTypes.
- Added internal sorting bitmaps, avoids loading the image files into the Files folder.
- Added two new properties: SortBitmapWidth and SortBitmapColor.
- Added SetCustomSortingBitmaps method, which allows to use custom bitmaps instead of the internal ones.
Attention: You need to open and close the Designer to make the new properties active.
Attention: You need to invert the parameters in JumpToRowAndSelect.

EDIT: 2019.07.04 Version 3.02
Amended error reported in post #887

EDIT: 2019.06.26 Version 3.01
Amended SingleLine property setting in the code

EDIT: 2019.04.05 Version 3.00
Amended SetColumnColors and SetTextColors
Removed Reflection library dependency

EDIT: 2018.04.11 Version 2.29
Version 2.27
set the two variables sortedCol and sortingDir to Public instaed of Private
added RemoveAccent routine for sorting with accented characters
Version 2.28
Added SetHeaderTypeFaces
Added SortRemoveAccents property
Version 2.29
Added SaveTableToCSV2 with a user defined separator character

EDIT: 2018.04.11 Version 2.26
added LoadSQLiteDB3 method using SQLExec2 instead of SQLExec
The query can include question marks which will be replaced with the values in the array.

EDIT: 2018.03.27 Version 2.25
amended minor errors
added UpdateCell method

EDIT: 2017.11.19 Version 2.22
improved JumpToRowAndSelect scrolls horizontally to the selected column
improved setHeaderHeight
added padding for status bar Label

EDIT: 2017.06.27 Version 2.19
Replaced DoEvents by Sleep(0)
Asked HERE

EDIT: 2017.06.27 Version 2.19
Replaced DoEvents by Sleep(0)
Asked HERE

EDIT: 2017.05.16 Version 2.18
Amended error reported HERE.

EDIT: 2017.03.09 Version 2.17
Amended error reported HERE.

EDIT: 2017.03.09 Version 2.15
Amended error reported here, Event signatures
#Event: CellClick(col As Int, row As Int)
#Event: CellLongClick(col As Int, row As Int)

EDIT: 2016.12.05 Version 2.14
Added NumberOfColumns and NumberOfRows as Public variables.
Amended error reported here.

EDIT: 2016.12.05 Version 2.13
Amended error reported here.
Added NumberOfColumns as a property for the Designer.

EDIT: 2016.07.30 Version 2.10
Amended error with TextAlignment and HeaderTextAlignment reported in post #606

EDIT: 2016.03.15 Version 2.00
Added CustomView support.
This version can be compiled into a library.
Changes between the previous versions and version 2.00
For a Table added in the Designer, this is new
No need to initialize nor add it onto a parent view
'For a Table added in the Designer, this is new
'No need to initialize nor add it onto a parent view

For a Table added in the code:
The Initialize routine has been splittend into two routines.
New:
Initialize (CallBack As Object, EventName As String)
InitializeTable (vNumberOfColumns As Int, cellAlignement As Int, showStatusL As Boolean)

'Example:
Table1.Initialize(Me, "Table1")
Table1.InitializeTable(5, Gravity.CENTER_HORIZONTAL, True)


Old:
Initialize(CallBack As Object, EventName As String, vNumberOfColumns As Int, cellAlignement As Int, showStatusL As Boolean)
Example:
Table1.Initialize(Me, "Table1", 5, Gravity.CENTER_HORIZONTAL, True)

EDIT: 2015.04.29 Version 1.43
As the modifications in LoadSQLiteDB don't work in all cases I went back.
LoadSQLiteDB as in version 1.40
Added LoadSQLiteDB2 where the column data types must be given.

EDIT: 2015.04.26 Version 1.42
Changed he LoadSQLiteDB routine, version 1.41 didn't work as expected.
The final solution was suggested by cimperia HERE.

EDIT: 2015.04.16 Version 1.41
Changed the LoadSQLiteDB routine according to the error reported in the SQL issue thread
and the SQLite Cursor GetString versus GetDouble thread.
The problem appears with numbers bigger than 999999.
I left version 1.40 in case of problems.
I tested it with a few databases, but I am not sure if it works in all cases.

EDIT: 2015.03.05
Amended bugs reported in posts #383 and #386
Added SetAutomaticWidths routine

EDIT: 2015.02.19
Amended the problem alignment reported in post # 378

EDIT: 2015.02.13
Amended the problem of rows not shown reported in post # 371

EDIT: 2015.01.09
Added header aligments

EDIT: 2014.08.14
Added HeaderHeight property
Amended RowColor problem reported in post #260

EDIT: 2014.08.10
Added SortColumn property asked in post #266
Added UseColumnColors ColumnColors and HeaderColors propeties

EDIT: 2014.05.10 Added RowHeight as a property

Screenshot:

1589638570453.png
 

Attachments

  • TableV1_44.zip
    44.8 KB · Views: 2,218
  • 1589638550715.png
    1589638550715.png
    31.8 KB · Views: 1,641
  • TestFastScroll.zip
    50.6 KB · Views: 1,488
  • TableV3_35.zip
    110 KB · Views: 308
  • Table.bas
    152.2 KB · Views: 225
Last edited:

Sergio Castellari

Active Member
Licensed User
I never loaded data from MySQL to B4x but I think that there should also be a MySql-builtin-function to format an internal datetime value into a string.

Hi @Kanne,

Of course it does.
But when using jRDC2 to get the query, jRDC2 returns the DATE fields as TICKS. So this routine simply formats the date as we wish.

Cheers
 

artsoft

Active Member
Licensed User
Longtime User
Hey Klaus!
Hey Kanne!

Sorry for the late response.... I was very busy the last days.
All my problems are fixed now - thanks for all your tips and the new versions of Table.bas. :)

________________________________________

The current issue I have is the choice of the correct text size - based on the given smart device or tablet.

Using this structure:

B4X:
    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues

I have the width, the height and the scale of the device's resolution.

________________________________________

My row height (the header height as well) are based on the given %y:

B4X:
    Table1.RowHeight = Ceil(4%y)
    Table1.HeaderHeight = Table1.RowHeight

This works great and looks perfect on each test device.


This works also on each device:

B4X:
Table1.RowHeight = Ceil(Table1.Height / 10)

So I can see always 10 rows on each device :)


_____________________________

BUT...

The question is now on how to calculate the correct Text size:

B4X:
    Dim ts As Int
    ts = Table1.RowHeight * 0.4
    Table1.TextSize = ts

My problem:
This works ok on one device and after switching to another device (in example to tablet) I see that this calculation is really wrong!



Therefore my question:
Is there a better method which can be used to calculate the correct text size - together with scale value or something like this??

Your answer is highly appreciated.

Best regards
ARTsoft

P.S.: The cell alignment is set to left and vertical center (with Bit.OR method).
 
Last edited:

Kanne

Member
Licensed User
Longtime User
you may search the forum by something like "calculation textsize".
I found Calculate textsize depending on label height and tested it with setting rowheight to 1/5 of table:
B4X:
    tableDes.RowHeight = Floor((tableDes.Height / 5))
    tableDes.TextSize =  Floor(tableDes.RowHeight * 500 / 1000dip)
In this case the font will fill about 50% of the cellheight which looks ok in my example - but you may test with other values.

@klaus:
setting the textsize after filling does not change the textsize for the fixed columns
 

artsoft

Active Member
Licensed User
Longtime User
you may search the forum by something like "calculation textsize".
I found Calculate textsize depending on label height and tested it with setting rowheight to 1/5 of table:
B4X:
    tableDes.RowHeight = Floor((tableDes.Height / 5))
    tableDes.TextSize =  Floor(tableDes.RowHeight * 500 / 1000dip)
In this case the font will fill about 50% of the cellheight which looks ok in my example - but you may test with other values.

@klaus:
setting the textsize after filling does not change the textsize for the fixed columns

Thanks Kanne for the tip!

Yes I also found out that the property textsize can only set before loading the data - after loading there is no chance to "resize" the text in order to adapt it to the given device resolution. This might be a really good feature.

Best regards
ARTsoft
 

Kanne

Member
Licensed User
Longtime User
Hi,

within my project it was possible in conjunction with .RefreshTable (only the bug with fixed columns).

I got an error when setting RowHeight before filling, but you can set Textsize before and Rowheight after - you know all values:
B4X:
tableDes.TextSize =  Floor(Floor((tableDes.Height / 5)) * 500 / 1000dip)
... (filling table)
tableDes.RowHeight = Floor((tableDes.Height / 5))
 
Last edited:

Kanne

Member
Licensed User
Longtime User
Hi @Kanne,

Of course it does.
But when using jRDC2 to get the query, jRDC2 returns the DATE fields as TICKS. So this routine simply formats the date as we wish.

Cheers

I can't imagine that jRDC2 will return ticks if you use
DATE_FORMAT(docdate, '%d/%m/%Y') AS Date_DMY
wihtin the SQL-statement.
The datefield will be formated as string and the middleware will give you a string.
Could you please test it for me, because I don't have a mySQL-DB und webservice to do that ... it only reflects my knowlegde of SQL.
 

artsoft

Active Member
Licensed User
Longtime User
you may search the forum by something like "calculation textsize".
I found Calculate textsize depending on label height and tested it with setting rowheight to 1/5 of table:
B4X:
    tableDes.RowHeight = Floor((tableDes.Height / 5))
    tableDes.TextSize =  Floor(tableDes.RowHeight * 500 / 1000dip)
In this case the font will fill about 50% of the cellheight which looks ok in my example - but you may test with other values.

@klaus:
setting the textsize after filling does not change the textsize for the fixed columns

@Kanne: Hi, I see that the factor (or better: the divisor) with value 5 is ok when I test the table on a smartphone. But on a tablet, this factory is too small. 5 entries on a smartphone provides a good text size - but on a test tablet .... OMG .... the table has a big height (the table covers about 80% of the screen and therefore I have a big height) and divided by 5 ... the row height is too high indeed.

I guess that we have to calculate this divisor as well ... while using the scale value of a screen.

Regards
ARTsoft
 

Kanne

Member
Licensed User
Longtime User
sure ... 5 rows is what you posted and asking how to fit the textsize according to the new rowheight.
You may calculate and set rowheight and the corresponding textsize as you want, e.g. to a readable size for you on the specific device.
 

Sergio Castellari

Active Member
Licensed User
I can't imagine that jRDC2 will return ticks if you use wihtin the SQL-statement.
The datefield will be formated as string and the middleware will give you a string.
Could you please test it for me, because I don't have a mySQL-DB und webservice to do that ... it only reflects my knowlegde of SQL.

Hi @Kanne !!

What you show is correct. In that case, deliver the date in a string.
My APP is sharing information with an ERP system in real time. This is why I don't want to modify queries (if not necessary) to maintain a certain order.
Also, I don't have that much knowledge. But I love B4A as it is helping me to get my APP.
Cheers!
 

Kanne

Member
Licensed User
Longtime User
Hi Klaus,
I tested a little with adjustment of the textheight / rowheight and found some minor bugs:
- "ScaleTable" seems not to scale the headerheight; could be solved by setting HeaderHeight to RowHeight after filling
- changing RowHeight after filling does not adjust panelheight so I have a black area at the end (when reducing) or can't scroll to the end (when increasing)
 

klaus

Expert
Licensed User
Longtime User
I was also looking at the ScaleTable method and improved it.
The ScaleTable method must be called before filling the Table.
Can you please test the attached Table.bas file.
 

Attachments

  • Table.bas
    122.3 KB · Views: 259

Kanne

Member
Licensed User
Longtime User
I made some test with my Table-Test-project:
V1: showing default layout (upper table runtime, lower table designer)
V2: tableRun.ScaleTable(1.5,1.5,False): scale is bigger than 1.5, also I don't want to change width / height of table, only labelsize / textsize
V3: tableRun.ScaleTable(1.5,1.5,False) & reset table width & height (before filling): scale is bigger than 1.5
V4: tableRun.ScaleTable(1.5,1.5,true): new header-hight not calculated for top of the rows
V5: tableRun.ScaleTable(1.5,1.5,true) & HeaderHeight = RowHeight (after filling): that is what I want

I don't know what the 3rd boolean-param is used for: when do I have to set it to which value ?
 

Attachments

  • V1.jpg
    V1.jpg
    58.8 KB · Views: 265
  • V2.jpg
    V2.jpg
    67.8 KB · Views: 274
  • V3.jpg
    V3.jpg
    63.8 KB · Views: 288
  • V4.jpg
    V4.jpg
    63 KB · Views: 275
  • V5.jpg
    V5.jpg
    61.7 KB · Views: 286
  • B4A Test Table.zip
    30.9 KB · Views: 247

klaus

Expert
Licensed User
Longtime User
Thank you for testing !
I added the ScaleTable method a long time ago, when anchors didn't exist.
At that time I wrote the Scale module, shortly after AutoScale was added in the Designer, allowing AutoScale also for views added in the code. This is mostly no more used.
The ScaleTable method was added after the Scale module to allow scaling Tables in conjunction with the Scale module, the third parameter depends if the ScaleAll method from the Scale module was used or not.
In your cases always False.

But, the ScaleTable method rescales all views in the Table!
Not sure that it will work OK with anchors.
Therfore, if you want to change only the heights and text sizes you must use the Height and TextSize properties or methods and not the ScaleTable method.
 

Diceman

Active Member
Licensed User
Do you think a column in FlexibleTable could handle wordwrapped text? When displaying data from databases I often have to display the first 'n' lines of paragraph text in a grid. It is usually 2 or 3 lines and if that is not enough then the text is truncated in the cell to 'n' lines and the usual "..." is appended to the end of the displayed text so the user knows there is more text.

Without wordwrap then the paragraph text would occupy too much horizontal space. I'd rather sacrifice a little vertical space instead. I would specify the cell property to display up to '3' rows and the max column width is 300dip across.

Is a future version of FlexibleTable capable of doing this?
TIA
 
If anybody will have the need to load JSON Data in the Table, I wrote a Sub that does it:
(Based on flexible Table Version 3.21)

LoadFromJson:
'load data from a JSON String

'load data from a JSON String
'JsnStr = String
'FieldArr = Array of shown Fields (of String), maybe less than fields in the JSON
'AutomaticWidths  True > set the column widths automaticaly

Public Sub LoadFromJson(JsnStr As String, FieldArr() As String, AutomaticWidths As Boolean)

    cAutomaticWidths = AutomaticWidths
    mNumberOfColumns = FieldArr.Length

    Dim Jsnpsr As JSONParser
    Dim JsnObjList As List
    Dim Headers(mNumberOfColumns) As String
    Dim ColumnWidths(mNumberOfColumns) As Int
    Dim HeaderWidths(mNumberOfColumns) As Int
    Dim DataWidths(mNumberOfColumns) As Int
    Dim cColumnDataType(mNumberOfColumns) As String
    Dim col, row As Int
    Dim str As String
    

    innerClearAll(mNumberOfColumns, True)
    Jsnpsr.Initialize(JsnStr)
    Try
        'assuming that the top level value is an array - everything is okay
        JsnObjList = Jsnpsr.NextArray
    Catch
        Try
            'assuming that the top level value is an object, then make it to an array
            JsnObjList.Initialize
            JsnObjList.Add(Jsnpsr.NextObject)
        Catch
            Log(LastException)
            Return
        End Try
    End Try
    
    For col = 0 To mNumberOfColumns - 1
        cColumnDataType(col) = "TEXT"
        Headers(col) = FieldArr(col)
        If AutomaticWidths = False Then
            ColumnWidths(col) = 130dip
            HeaderWidths(col) = 130dip
            DataWidths(col) = 130dip
        Else
            If MultiTypeFace = True Then
                HeaderWidths(col) = cvs.MeasureStringWidth(Headers(col), cHeaderTypeFaces(col), cTextSize) + ExtraWidth
            Else
                HeaderWidths(col) = cvs.MeasureStringWidth(Headers(col), cHeaderTypeFace, cTextSize) + ExtraWidth
            End If
            DataWidths(col) = 0
            For row = 0 To JsnObjList.Size - 1
                str = FieldArr(col)
                If str <> Null Then
                    If MultiTypeFace = True Then
                        DataWidths(col) = Max(DataWidths(col), cvs.MeasureStringWidth(str, cTypeFaces(col).DEFAULT, cTextSize) + ExtraWidth)
                    Else
                        DataWidths(col) = Max(DataWidths(col), cvs.MeasureStringWidth(str, cTypeFace, cTextSize) + ExtraWidth)
                    End If
                End If
            Next
            ColumnWidths(col) = Max(HeaderWidths(col), DataWidths(col))
        End If
    Next
    SetHeader(Headers)
    SetColumnsWidths(ColumnWidths)

    'For row = 0 To JsnObjList.Size - 1
    For Each colroot As Map In JsnObjList
        Dim R(mNumberOfColumns), str As String
        For col = 0 To mNumberOfColumns - 1
            str = colroot.Get(FieldArr(col))
            If str <> Null Then
                R(col) = str
            Else
                R(col) = ""
            End If
        Next
        AddRow(R) 
    Next
'    Log(Data.Size)
End Sub

End Sub
 
Last edited:
.. and here is a little example
(Don't forget to include the JSON Library in your project)

Example for LoadFromJson:
Sub Activity_Create(FirstTime As Boolean)
   
   
    Dim jsnstr As String = "[{""ArtNr"":""12345"",""Desc"":""Chair"",""Material"":""Wood"",""Color"":""Green""},{""ArtNr"":""22331"",""Desc"":""Table"",""Material"":""Wood"",""Color"":""Yellow""},{""ArtNr"":""98765"",""Desc"":""Lamp"",""Material"":""Steel"",""Color"":""None""},{""ArtNr"":""77777"",""Desc"":""Window"",""Material"":""Glass"",""Color"":""transparent""}]"
    Dim FieldNames() As String = (Array As String("ArtNr", "Desc", "Material", "Color"))
   
    Activity.LoadLayout("MyNewLayout")
    Table1.LineWidth = 3dip
    Table1.FastScroll = True
    Table1.FastScrollMinItems = 15
    Table1.FastScrollColumnIndex = 2
    Table1.LoadFromJson(jsnstr, FieldNames, True)
    Table1.SetColumnsWidths(Array As Int(150dip, 300dip, 300dip, 300dip))
End Sub
screenshot.PNG
 
Last edited:
Top