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

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

SS-2012-07-04_10.38.01.png


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

  • Table.zip
    14.9 KB · Views: 7,913
Last edited:

jeffnooch

Member
Licensed User
Longtime User
I created a table and it works great...
i'm also adding some labels, populating the text and formatting like this:


B4X:
        btnClose.Initialize("btnClose")
        lblP1.Initialize("lblP1")
        pnlScores.AddView(btnClose,45%x,90%y,10%x,9%y)
        pnlScores.AddView(lblP1,0,0,45%x,7%y)
       
        lblP1.Text=p1 & " " & p1AvgLbl : lblP1.TextSize=27 : lblP1.Typeface = Typeface.DEFAULT_BOLD :             lblP1.TextColor=txtP1.TextColor

everything looks great the first time i open up the panel
but p1AvgLbl changes and if i open it a 2nd time it looks like the text in the field is overlaying the text that was there the first time it opened...

any thoughts?

thanks
 

klaus

Expert
Licensed User
Longtime User
This is not related to the TableView.
Unfortunately you don't give enough information.

I suppose that you call the code you show several times.
This means that every time you call this code you add a new Label.
As Labes are, by default, transparent you'll see also the texts of all underlaying Labels.
You should add the Label only once and the change only the Text property.

There exist a newer version of the Table Class.
 
Last edited:

jeffnooch

Member
Licensed User
Longtime User
Klaus,
thanks for the reply

yeah the panel was created to show my tables...then i add the labels (essentially as a header for each table shown)...
and yes i have a menu item that can be opened many times which brings up the panel/table/labels
so i guess my next question would be how can i handle it so i only add the label once?
is there a way to check for the existence of the label or another way to handle?

any thoughts would be great...

PS...thanks i'll check out the newer table class when i get a chance...
 

ValDog

Active Member
Licensed User
Longtime User
I am probably having a senior moment, but - how could I use the SaveTableToCSV function to save a table (w/o headers) to a string - instead of a file?
 

ValDog

Active Member
Licensed User
Longtime User
Why do you want to transform the table into a string?
I am storing the table data in a SQLite field, using comma delimited sets (rows) of data. I've been able to write a function to do this - thanks for the response.
 

ValDog

Active Member
Licensed User
Longtime User
Klaus, I've already solved my problem. I was only trying to get the table data into a text field in a way that it could be later read and parsed for use in another module.

Thanks!
 

slavko

Member
Licensed User
Longtime User
Hi,

with great help of examples i did an application for inventory counting. To make more obvious, what cell we need to update, i want to use sentence Table1.SelectedCellColor = Colors.Yellow. My problem is, that no matter, where i put it in code, there is no effect.

Do i need to enable something else to make this work or where this part of coud should be located?

Thx, Slavko
 

klaus

Expert
Licensed User
Longtime User
What version of the class are you using ?
The latest version is 1.43 HERE.
You should put Table1.SelectedCellColor = Colors.Yellow after Table1.Initialize and before filling the Table.
I just tested it in the demo program and it works.
 

slavko

Member
Licensed User
Longtime User
First i replaced 1.37 version with latest one 1.43.

line
Table1.SelectedCellColor = Colors.Yellow is between:
Table.Initialize
and part, where i fill data in:
Table1.LoadTableFromCSV2(File.DirRootExternal & "/Download/", "zaloge.csv", True, ";",True)

in that part i have also other definitions for table eg:
Table1.HeaderTextColor = Colors.Yellow
Table1.TextColor = Colors.Blue
Table1.TableColor = Colors.black

and they are visible -they are working

Should i add a code?
 

klaus

Expert
Licensed User
Longtime User
Without seeing your code it's almost impossible to give a concrete advice.

In the demo program I have this, which works.
B4X:
    Table1.Initialize(Me, "Table1", 5, Gravity.CENTER_HORIZONTAL, True)
    Table1.CellAlignment = Bit.OR(Gravity.LEFT, Gravity.CENTER_VERTICAL)
    Table1.HeaderColor = Colors.Blue
    Table1.HeaderTextColor = Colors.Yellow
    Table1.TextColor = Colors.Blue
    Table1.TableColor = Colors.Red
    Table1.RowHeight = 40dip
'    Table1.RowColor1 = Colors.RGB(244,164,96)
'    Table1.RowColor2 = Colors.RGB(255, 153, 255)
  
    Table1.SelectedCellColor = Colors.Yellow
'    Table1.SelectedRowColor = Colors.RGB(255,239,213)
'    Table1.AllowSelection = False
    Table1.AddToActivity(pnlTable, 0, 0, pnlTable.Width, pnlTable.Height)  
  
'    Table1.MultiSelect = True
    Table1.MultiSelect = False
    Table1.SortColumn = False
    Table1.SingleLine = False  
  
'    FillTableFix
'    FillTableSQLite
    FillTableCSV
 

slavko

Member
Licensed User
Longtime User
I added code. Did try to move Table1.SelectedCellColor around , but no luck.

What i did wrong?
Also, why
Table1.UseColumnColors = True
is causing dump if set to True?
 

Attachments

  • Union_tablica.zip
    16.6 KB · Views: 315

slavko

Member
Licensed User
Longtime User
With moving around i found a proper place for definition of colours.

Now, i am having issue with
Table1.SetColumnsWidths(Array As Int(100dip,...,100dip))

code is positioned in the middle of Table1 definintions:
pnlTable.Initialize("")

Activity.AddView(pnlTable, 20dip, 20dip, 100%x - 40dip, 100%y - 200dip)

Table1.Initialize(Me, "Table1", 18, Gravity.CENTER_HORIZONTAL, True)

Table1.AddToActivity(pnlTable, 0, 0, pnlTable.Width, pnlTable.Height)

Table1.SetColumnsWidths(Array As Int(100dip,300dip,200dip,100dip,70dip,5dip,100dip,100dip,100dip,100dip,100dip,100dip,100dip,100dip,100dip,100dip,100dip))

Table1.LineWidth = 10dip
Table1.TextSize = 30
'Table1.UseColumnColors = True
Table1.CellAlignment = Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL)


During run i am receiveing error:
table_setcolumnswidths (B4A line: 275)
v.Width = w
java.lang.RuntimeException: Object should first be initialized (View).
Did you forget to call Activity.LoadLayout?

What is wrong now?
 

slavko

Member
Licensed User
Longtime User
java.lang.RuntimeException: Object should first be initialized (View).
Did you forget to call Activity.LoadLayout?

A bit misleading message for me. So after fill table with values, SetColumnsWidths works.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello to all,
when I click on a particular cell change color (ok it works), but how much change the line resets the standard color.
Is there a way to preserve the color and prevent spade changed the color of the cell?

upload_2015-11-21_22-52-26.png


upload_2015-11-21_22-55-11.png


Use this function for change the color

Sub Table1_CellClick(Col As Int, Row As Int)

If mTableOpenOrder.visibleRows.ContainsKey(Row) Then
Dim lbls() As Label
lbls = mTableOpenOrder. visibleRows.get(Row)
lbls(3).Color = Colors.Green
End If

end sub
 

Attachments

  • upload_2015-11-21_22-54-14.png
    upload_2015-11-21_22-54-14.png
    18.8 KB · Views: 281

Erel

B4X founder
Staff member
Licensed User
Longtime User
TableView is optimized for large tables. It recycles the views.
You cannot just change the label color as the same label will be later used for a different cell.

If the table is not too large and you need more customization then it will be easier to implement a similar table with CustomListView (which doesn't
reuse views).
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello Erel
thank you, I understand.
But custom listview has some features that I need:

- Sorting columns
- Horizontal scrolling

Find an alternative solution.
Greetings
 
Top