B4A Library [Class] Flexible Table

Yafuhenk

Active Member
Licensed User
Hi Klaus,

Thanks for your reply.
Before aksing time from you let me first check the LoadSQLiteDB sub.
I see that it works with queries.
Maybe I can use this sub instead.
I not I will post here again.

Henk
 

klaus

Expert
Licensed User
Try the attached version 1.39 of the Table class.
I added a SetAutomaticWidths routine to the Class and updated the AddRowAutomaticWidth.
Using AddRowAutomaticWidth to fill the table is not the best approach.
You should fill the table with AddRow and when you are finished call SetAutomaticWidths.
Then afterwards when you want to add a row use AddRowAutomaticWidth.
 

Attachments

Yafuhenk

Active Member
Licensed User
Hi Klaus,

Thanks I will have a look.
The LoadSQLLiteDB sub works fine for me as far as I can see now.

By the way, you should change line 627 (Public Sub SetHeader)
B4X:
Header.AddView(L, Left, 0, w, cRowHeight)
into
B4X:
Header.AddView(L, Left, 0, w, cHeaderHeight)
Regards

Henk
 

Yafuhenk

Active Member
Licensed User
I found another bug in this class but this one I can't fix.
Maybe some one can help

Bug description: Can't select the last row(s)
Occurs when
1) cHeaderHeight > ± 1,6 x cRowHeight AND
2) the number of rows in the table is so small that vertical scroll isn't possible / needed

In the attached file I've set cRowHeight to 25 and cHeaderHeight to 50
In this case it's not possible to select the last row (= row 1)
If you set cRowHeight to 25 and cHeaderHeight to 75 isn't possible to select the last two rows (row 0 and row 1)

Any help is welcome

Henk
 

Attachments

LucaMs

Expert
Licensed User
I found another bug in this class but this one I can't fix.
Maybe some one can help

Bug description: Can't select the last row(s)
Occurs when
1) cHeaderHeight > ± 1,6 x cRowHeight AND
2) the number of rows in the table is so small that vertical scroll isn't possible / needed

In the attached file I've set cRowHeight to 25 and cHeaderHeight to 50
In this case it's not possible to select the last row (= row 1)
If you set cRowHeight to 25 and cHeaderHeight to 75 isn't possible to select the last two rows (row 0 and row 1)

Any help is welcome

Henk

I'm not so sure, but probably the line 1147:
B4X:
internalPanel.Top = Data.Size * cRowHeight + cRowHeight

should be:
B4X:
internalPanel.Top = Data.Size * cRowHeight + cHeaderHeight
 

Wembly

Member
Licensed User
Apologies if already answered - but does this class allow you to make the first column a fixed position column so always present on screen when scrolling across the all the fields?
 

cimperia

Active Member
Licensed User
Klaus

thank you for maintaining this class. Lucas' method could be slightly amended to accept a Map (of columns) and List (of rows) arguments instead of a DBResult object, making this function easy to integrate into the Table class?

If using the RDC module, one could call the LoadRDCResult this way :
B4X:
LoadRDCResult(DBResult.Columns, DBResult.Rows, True)
What do you think?

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
 

cimperia

Active Member
Licensed User
Ok thanks, but I won't take credit for the code as I have just adjusted it to its new type of parameters. Without referring to RDC, I think it might be a worthwhile addition to the class when you have a set of columns and list of rows to display.
 

Tom1s

Member
Licensed User
Hi

I can't figure out why this is not working:
I load rows to sqllite with RDC then I read those on table. When user presses Table1_celllongClick row can be removed. After one row is removed I get an error if trying to remove another row. Row order gets scrambled or something.

B4X:
Sub Table1_celllongClick (Col As Int, Row As Int)
    Dim t As String
    Activity.Title = Table1.GetValue(Col, Row)
    Dim Row2 As Int = Row+1
    Dim Listofmaps As List
    Listofmaps.Initialize
    Dim m As Map
    m.Initialize
    m=DBUtils.Executemap(SQL1,"Select * FROM LoadedT WHERE ROWid = "&Row2, Null)
    t= m.Get("tname")
    Listofmaps.Add(m)
    DBUtils.InsertMaps(SQL1,"UnloadedT",Listofmaps) 'and put in new data
    DBUtils.DeleteRecord(SQL1,"LoadedT",m)
    'Table1.RemoveRow(Row2)
    FillTableLT
B4X:
Sub FillTableLT
'    Table1.ClearAll
    Table1.Initialize(Me, "Table1", 3, Gravity.CENTER_HORIZONTAL, True)
    Table1.CellAlignment = Bit.OR(Gravity.center, Gravity.CENTER_VERTICAL)
    Table1.HeaderColor = Colors.BLACK
    Table1.HeaderTextColor = Colors.White
    Table1.TextColor = Colors.white
    Table1.TableColor = Colors.Gray
    Table1.RowHeight = 50dip
    Table1.RowColor1 = Colors.Gray
    Table1.RowColor2 = Colors.LightGray
    Table1.SelectedCellColor = Colors.blue
    Table1.SelectedRowColor = Colors.Blue
'    Table1.AllowSelection = True
    Table1.AddToActivity(panel1, 0, 0, panel1.Width, panel1.Height)
'    Table1.MultiSelect = True
    Table1.MultiSelect = False
    Table1.SortColumn = False
    Table1.SingleLine = False
'    Table1.SetAutomaticWidths
    Dim Query As String
'    Table1.ClearAll


    Table1.LineWidth = 2dip
    Table1.TextSize = 14
'     Dim tf() As Typeface
'    tf = Array As Typeface(Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT)
'    Table1.SetTypeFaces(tf)



    Query = "SELECT * FROM " & "LoadedT"
    Table1.LoadSQLiteDB(SQL1, Query, True)

    Table1.SetColumnsWidths(Array As Int(20%x, 35%x, 45%x))

End Sub
Error:

ExecuteMap: Select * FROM LoadedT WHERE ROWid = 1


No records found.
ExecuteMap: Select * FROM LoadedT WHERE ROWid = 1
No records found.
(Map) Not initialized
Error occurred on line: 423 (main)
java.lang.RuntimeException: Object should first be initialized (Map).
....

SO it cant find the row after I have deleted some of the rows...
 
Last edited:

klaus

Expert
Licensed User
The error message says :
(Map) Not initialized
java.lang.RuntimeException: Object should first be initialized (Map).

This means that you are using a Map which is not initialized.
This is not related to the Table Class.

Have you seen the LoadRDCResult routine, written by LucaMs, in the first post ?
You can fill the table directly with RDC.
 
Last edited:

LucaMs

Expert
Licensed User
The error message says :
(Map) Not initialized
java.lang.RuntimeException: Object should first be initialized (Map).

This means that you are using a Map which is not initialized.
This is not related to the Table Class.



You can not compare ROWid and Table Row, they are two different things.
 
Top