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,617
Last edited:

TheWind777

Active Member
Licensed User
Longtime User
I have your latest Class version 1.19... but is there a working example that goes along with it?

I tried loading it into one of the earlier examples and I get an error about the reflector library, and once I've included it, an error

There are so many versions of so many experiments, and people uploading bug-ridden examples, that I don't know which is a good example to use which goes with the latest class module.

The code which gives the error is:
(But using other examples also gave same errors).

B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: True
   #ApplicationLabel: Table
   #VersionCode: 1
   #VersionName:
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
   
End Sub

Sub Globals
   Dim Table1, Table2 As Table
   Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Button1.Initialize("Button1")
   Activity.AddView(Button1, 65%x, 80dip, 100dip, 50dip)
   Button1.Text = "Hide"
   
   Table1.Initialize(Me, "Table1", 4)
   Table1.AddToActivity(Activity, 10dip, 10dip, 48%x - 10dip, 60%y)   
   Table1.SetHeader(Array As String("Col0", "Col1", "Col2", "Col3"))
   For i = 0 To 5000
     Table1.AddRow(Array As String("Row: " & i, "ccc", "ddd", "eee"))
   Next
'   Table1.SetColumnsWidths(Array As Int(100dip, 30dip, 30dip, 100%x - 160dip))
   Table1.SetColumnsWidths(Array As Int(100dip, 130dip, 130dip, 130dip))
   Activity.AddMenuItem("Jump To 3000", "Jump1")
   Activity.AddMenuItem("Jump To 0", "Jump2")
   
   Table2.Initialize(Me, "Table2", 0)
   Table2.AddToActivity(Activity, 52%x, 40%y, 48%x, 60%y)
   Table2.LoadTableFromCSV(File.DirAssets, "citylist.csv", True)
   Table2.SetColumnsWidths(Array As Int(130dip, 180dip, 100dip, 100dip))
End Sub

Sub Jump1_Click
   Table1.JumpToRow(3000)
End Sub

Sub Jump2_Click
   Table1.JumpToRow(0)
End Sub

Sub ClearAll_Click
   Table1.ClearAll
   For i = 0 To 1000
     Table1.AddRow(Array As String("rrr: " & i, "ccc", "ddd", "eee"))
   Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Table2_CellClick (Col As Int, Row As Int)
   Log("(Table2) CellClick: " & Col & " , " & Row)
   Activity.Title = Table2.GetValue(Col, Row)
End Sub

Sub Table1_CellClick (Col As Int, Row As Int)
   Log("CellClick: " & Col & " , " & Row)
   Activity.Title = Table1.GetValue(Col, Row)
End Sub

Sub Table1_HeaderClick (Col As Int)
   Log("HeaderClick: " & Col)
End Sub

Sub Button1_Click
   If Button1.Text = "Hide" Then
     Table1.Visible(False)
     Button1.Text = "Show"
   Else
     Table1.Visible(True)
     Button1.Text = "Hide"
   End If
End Sub
 

mat2175

Member
Licensed User
Longtime User
Hi i'm not sure how to get the row number(index) once the user selects a row in the table. Any advice would be grateful thanks.
 
Last edited:

mat2175

Member
Licensed User
Longtime User
Well that's not what i'm looking for thanks anyway. I want to remove the row that the user selects using a button click event.
 

Reinierus

Member
Licensed User
Longtime User
Change Activity to Panel

Hello.
As can I see, to put the table to a panel, we only need to make this:

B4X:
'Adds the tablet to the activity.
Public Sub AddToActivity(Act As Panel, Left As Int, Top As Int, Width As Int, Height As Int)
    visible = True
    pnlTable.Initialize("")
    Act.AddView(pnlTable, Left, Top , Width, Height)

    Header.Initialize("")
    Header.Color = TableColor
    pnlTable.AddView(Header, 0, 0, Width, RowHeight)
    pnlTable.AddView(SV, 0, RowHeight, Width, Height - RowHeight)
    ColumnWidth = SV.Width / NumberOfColumns
   
    SV_ScrollChanged(0, 0)

End Sub

Then in the Activity...

B4X:
Panel1.Initialize("")
    Panel1.RemoveAllViews
    TablaLugares.Initialize( Me , "Tablalugares", 3)
    'Panel1.AddView(TablaLugares,0,0,455,400)
   
    TablaLugares.AddToActivity( Panel1, 0, 0dip, 100%x, 50%y)   
    TablaLugares.SetHeader(Array As String("Lat", "Long", "Lugar"))
    For i = 0 To 500
        TablaLugares.AddRow(Array As String("Row: " & i, "ccc", "eee"))
    Next
    TablaLugares.SetColumnsWidths(Array As Int(100dip, 30dip, 130dip, 100%x - 260dip))

But this error occurs:

** Activity (main) Create, isFirst = true **

java.lang.RuntimeException: Object should first be initialized (View).


at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4a.objects.ViewWrapper.setWidth(ViewWrapper.java:123)
at Kilometreador.reinier.table._setcolumnswidths(table.java:695)
at Kilometreador.reinier.main._muestralugares(main.java:699)
at Kilometreador.reinier.main._activity_create(main.java:470)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at Kilometreador.reinier.main.afterFirstLayout(main.java:98)
at Kilometreador.reinier.main.access$100(main.java:16)
at Kilometreador.reinier.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)


at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (View).

In the image you can see where is the error in the code by debugging.
Can you help me? What I doing wrong?

Thanks a lot
 

Attachments

  • Error Tabla.png
    Error Tabla.png
    122.7 KB · Views: 190

Reinierus

Member
Licensed User
Longtime User
Thanks Erel, I tried without change the class but I get the same error.

I will try with the most recent version and I will review my code.

Best regards
 

stu14t

Active Member
Licensed User
Longtime User
For some reason you have not initialized the the view V. It says so in the local variables window.
 
Last edited:

vinnythepooh71

Member
Licensed User
Longtime User
Sorry if i posted in the wrong place, but i 'd like to try this library and i was wondering if it was possible to add a small bitmap image in one o the grid's cells...
By the way thanks for this great lib
 

vinnythepooh71

Member
Licensed User
Longtime User
Is there a way i can achieve this ? i mean can i create a scrollview and populate with labels arranged in a grid form ? sorry if this sounds like a dumb question but i starting now with b4a..
 
Top