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

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jul 4, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    An improved version of this class is available here: http://www.basic4ppc.com/forum/addi...icial-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.

    [​IMG]

    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.
     

    Attached Files:

    Last edited: Sep 25, 2013
    Daniel-White, najm, cimperia and 3 others like this.
  2. mc73

    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?
     
  3. Erel

    Erel Administrator Staff 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).
     
  4. stefanoa

    stefanoa Active Member Licensed User

    Great! :sign0098:
     
  5. mc73

    mc73 Well-Known Member Licensed User

    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.
     
  6. Erel

    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.
     
  7. Informatix

    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.
     
  8. Informatix

    Informatix Expert Licensed User

    If you look at the Treeview class in this forum, you'll see it's fairly easy to add horizontal scrolling to a ScrollView.
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    You are correct. The uploaded project is now fixed.
     
  10. manios

    manios Active Member Licensed User

    1+
    Very helpfull!
     
  11. peacemaker

    peacemaker Well-Known Member Licensed User

    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:

    Code:
    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: Jul 4, 2012
  12. Erel

    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.
     
  13. peacemaker

    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.
     
  14. Erel

    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.
     
  15. manios

    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?
     
  16. Asmoro

    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.

    And..

    Is this Tableview also to use for Mysql database?
     
  17. Erel

    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.
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    You can change the type of Act parameter to Panel. It will work for both panels and activities.
     
  19. Mahares

    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.
     
  20. Erel

    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.
     
Loading...