B4A Library [Class] TableView - Supports tables of any size

Erel

Administrator
Staff member
Licensed User
An improved version of this class is available here: http://www.basic4ppc.com/forum/additional-libraries-classes-official-updates/30649-class-flexible-table.html


The Table class allows you to show tables of any sizes. The views (labels) are reused to avoid creating many views.

With the help of StringUtils the table can be loaded and saved to a CSV file. It shouldn't be difficult to show SQL tables using DBUtils.ExecuteMemoryTable.



Follow the attached example to see how the class is used. To add it to your own project you need to add the class module to your project and add a reference to StringUtils library.

RemoveRow code is available here: http://www.basic4ppc.com/forum/showpost.php?p=146795&postcount=147

V1.10 uploaded. New method: SetColumnsWidths. Allows you to manually set the width of each column
V1.11 uploaded. Fixes a bug with grey stripes appearing.
 

Attachments

Last edited:

mc73

Well-Known Member
Licensed User
Screenshot looks great! Haven't upgraded to v2 yet, still, just out of curiosity, did you use a webView for this?
 

Erel

Administrator
Staff member
Licensed User
did you use a webView for this?
No. It is a ScrollView with labels. As written above the labels are reused to avoid adding many labels (like the previous implementation).
 

mc73

Well-Known Member
Licensed User
No. It is a ScrollView with labels. As written above the labels are reused to avoid adding many labels (like the previous implementation).
I see. True, it's much more efficient to use just a tiny area of labels, I totally agree. Another one: Is this table scrollable horizontally? I remember an old discussion about this, when we were talking about choosing a webView when we want something like this.

Finally, as I've already said, I haven't yet upgraded. It's just a matter of a little 'fear' about compile error, while I'm in the middle of an important app for me. I hope my fears are not logical.
 

Erel

Administrator
Staff member
Licensed User
As it is based on ScrollView it doesn't support horizontal scrolling. If you want to create a WebView table then you can use DBUtils.ExecuteHtmlTable.
 

Informatix

Expert
Licensed User
Very useful class.

You should verify that the sub exists before doing CallSub. Currently, your demo raises an exception when you click on the header of the second table.
 

Informatix

Expert
Licensed User
Is this table scrollable horizontally? I remember an old discussion about this, when we were talking about choosing a webView when we want something like this.
If you look at the Treeview class in this forum, you'll see it's fairly easy to add horizontal scrolling to a ScrollView.
 

peacemaker

Well-Known Member
Licensed User
As it is based on ScrollView it doesn't support horizontal scrolling.
I'm sure than it needs that row.height is to be optionally auto-adjusted for longest cell content (maybe up to, say, 90% of ScrollView.Height, if scrolling problem), like i used:

B4X:
Sub AdjustHeight (Obj As Label)
Dim A As StringUtils, t As Int
t = A.MeasureMultilineTextHeight(Obj, Obj.Text)
Obj.Height = t
End Sub

'Adds a row to the table
Sub AddRow(Values() As String)
   If Values.Length <> NumberOfColumns Then
      Log("Wrong number of values.")
      Return
   End If
   Dim lastRow As Int, BiggestHeight As Int, Lprev, a As Label, RowHeight2, RowHeight2_1 As Int
   lastRow = NumberOfRows
   BiggestHeight = -1
   
   RowHeight2_1 = RowHeight_1
   If lastRow = 0 Then
      RowHeight2 = 0
   Else
      a = GetView(lastRow - 1,0)
      RowHeight2 = a.Top + a.Height + RowLineWidth
   End If
   
   For I = 0 To NumberOfColumns - 1
      Dim l As Label
      l.Initialize("cell")
      l.Text = Values(I)
      l.Gravity = Alignment
      l.TextSize = FontSize
      l.TextColor = FontColor
      l.Color=Colors.White
      Dim rc As RowCol
      rc.Initialize
      rc.Col = I
      rc.Row = lastRow
      l.Tag = rc

      tbl.AddView(l, ColumnWidth * I, RowHeight2, ColumnWidth_1, RowHeight2_1)
      AdjustHeight (l)
      
      If BiggestHeight < 0 Then   'first cell
         BiggestHeight = l.Height
         RowHeight2_1 = BiggestHeight
      Else If BiggestHeight < l.Height Then 
         BiggestHeight = l.Height
         Lprev.Height = BiggestHeight
         RowHeight2_1 = BiggestHeight
      Else If BiggestHeight > l.Height Then 
         l.Height = BiggestHeight
      End If
      Lprev = l   'latest cell
   Next
   tbl.Height = tbl.Height + BiggestHeight + 2dip 
End Sub
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Your code will not work correctly. You can set RowHeight before adding items. However all the rows must have the same height.
 

peacemaker

Well-Known Member
Licensed User
This code works in my project, adjusting whole the row (all labels) height for the text.
I meant, that such auto-adjusting for cell's text would be also useful in your class.
 

Erel

Administrator
Staff member
Licensed User
In this case it will be more complicated as the labels are reused. This significantly improves the table performance. The downside is that such customizations are more difficult.
 

manios

Active Member
Licensed User
Can Table also added to a panel?

The Table is added to an Activity. Would it be possible to add a Table also to panel?
 

Asmoro

Active Member
Licensed User
Hi Erel,

This is what I get trying to test it out.
Do I have to fill in somewhere to fix it.

Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'subexists' is used before it was assigned any value.
Occurred on line: 233
If SubExists(Callback, Event & "_CellClick") Then
And..

It shouldn't be difficult to show SQL tables using DBUtils.ExecuteMemoryTable
Is this Tableview also to use for Mysql database?
 

Erel

Administrator
Staff member
Licensed User
SubExists keyword was added in B4A v2.00. Either you are using a previous version or you have an old copy of core.jar / core.xml in your additional libraries path.

You can fill the table with any data you need. Including data that comes from a MySQL table.
 

Erel

Administrator
Staff member
Licensed User
The Table is added to an Activity. Would it be possible to add a Table also to panel?
You can change the type of Act parameter to Panel. It will work for both panels and activities.
 

Mahares

Well Known Member
Licensed User
How can I apply the sample code to loading the contents of a SQLite table instead of loading a CSV file. Do I need to convert the table to a CSV file, then load the CSV file as it is shown in this sample? There is a reference to DBUtils.ExecuteMemoryTable, but I have not used that before.
Thank you for any tips or snippets.
 

Erel

Administrator
Staff member
Licensed User
You do not need to convert it to CSV. The simplest solution is to go over the results and use Table.AddRow to add the data.
 
Top