B4A Library [Class] Flexible Table

fishwolf

Well-Known Member
Licensed User
Longtime User
Hi,

i need set the table from json data, in generaly i use the AddRow method

i'm try to replace function FillTableSQLiteNew with my FillTableFix for a simple test

B4X:
Sub FillTableFix
   Table1.ClearAll

   Table1.LineWidth = 3dip
   Table1.TextSize = 20

  Dim tf() As Typeface
   tf = Array As Typeface(Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT)
   Table1.SetTypeFaces(tf)
   
   Table1.AddRow(Array As String("aaa", "bbbb", "cccc", "dddd"))

End Sub

but i return a exception

B4X:
java.lang.RuntimeException: Object should first be initialized (View).
Did you forget to call Activity.LoadLayout?
   at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
   at anywheresoftware.b4a.objects.ViewWrapper.getWidth(ViewWrapper.java:128)
   at b4a.example.table._showrow(table.java:4321)
   at b4a.example.table._addrow(table.java:129)
   at b4a.example.main._filltablesqlitenew(main.java:661)
   at b4a.example.main._activity_resume(main.java:409)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)


   at anywheresoftware.b4a.BA.raiseEvent(BA.java:157)
   at b4a.example.main.afterFirstLayout(main.java:104)
   at b4a.example.main.access$100(main.java:16)
   at b4a.example.main$WaitForLayout.run(main.java:76)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)

Any suggestion ?

Thanks
 

fishwolf

Well-Known Member
Licensed User
Longtime User
i use the example of page one and only replace LoadSQLiteDb with AddRow

This code work fine
B4X:
Table1.LoadSQLiteDB(SQL1, Query, True)
Table1.AddRow(Array As String("aaa", "bbbb", "cccc", "dddd", "eeee"))

This code doesn't work
B4X:
Table1.AddRow(Array As String("aaa", "bbbb", "cccc", "dddd", "eeee"))

Thanks
 

klaus

Expert
Licensed User
Longtime User
Several problems.
When you run this line Table1.LoadSQLiteDB(SQL1, Query, True) the number of columns is set to 5 according to the number of columns in the database.
If you remvove this line:
- the number of columns is set to 4 in the initialization: Table1.Initialize(Me, "Table1", 4, Gravity.CENTER_HORIZONTAL, True).
- as you want 5 columns you must change the initialization with Table1.Initialize(Me, "Table1", 5, Gravity.CENTER_HORIZONTAL, True).
- you need to set the headers, these are automatically set with Table1.LoadSQLiteDB(SQL1, Query, True).
- you should also set the column widths.
- you must adapt the Table1_Click event routine to your needs or remove it.

The code below works:
B4X:
Sub FillTableFix
    Table1.LineWidth = 3dip
    Table1.TextSize = 20

  Dim tf() As Typeface
    tf = Array As Typeface(Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT)
    Table1.SetTypeFaces(tf)

    Table1.SetHeader(Array As String("Head1", "Head2", "Head3", "Head4", "Head4"))
    Table1.SetColumnsWidths(Array As Int(25%x, 25%x, 25%x, 25%x, 25%x))
    Table1.ClearAll

    Table1.AddRow(Array As String("aaa1", "bbbb1", "cccc1", "dddd1", "eeee1"))
    Table1.AddRow(Array As String("aaa2", "bbbb2", "cccc2", "dddd2", "eeee2"))
    Table1.AddRow(Array As String("aaa3", "bbbb3", "cccc3", "dddd3", "eeee3"))
End Sub

Attached a test project.
 

Attachments

  • TableV1_33_1.zip
    42.2 KB · Views: 246

fishwolf

Well-Known Member
Licensed User
Longtime User

Thank you very match
 

fishwolf

Well-Known Member
Licensed User
Longtime User
Sorry,

can i transform the table in a image for post to facebook ?

thank you
 

incendio

Well-Known Member
Licensed User
Longtime User
Dear Klaus,

First, I want to thank you for this class, it is a great help for me.

When I use this class, I have a question about method SaveTableToCSV.

These are my codes snippet
B4X:
Sub Send_Click
    Tbl.SaveTableToCSV(File.DirRootExternal,"data.csv")
    Dim ei As Email
    ei.To.Add("abc@gmail.com")
    ei.Body = "File"
    ei.Subject = "From Internal"
    ei.Attachments.Add(File.Combine(File.DirRootExternal, "data.csv"))
    StartActivity(ei.GetIntent)
End Sub

Those codes works well when data in Tbl only a few records.
But, when there are a lot numbers of data, how do I know that SaveTableToCSV already finished save all data?
 

klaus

Expert
Licensed User
Longtime User
First, I want to thank you for this class, it is a great help for me.
I'm glad that you like this class but to be clear, the original Class was written by Erel here.
Melamoud and myself, we have added some more features.

But, when there are a lot numbers of data, how do I know that SaveTableToCSV
Sorry, but I have never used SaveTableToCSV and don't know when SaveTableToCSV finishes.
Hopefully, Erel has an answer.

Why do you save the database to a csv file ?
 

Mahares

Expert
Licensed User
Longtime User
Perhaps you can use a timer to allow the file to be saved. Then initiate the email afte x number of seconds expire:
B4X:
Dim MyTimer As Timer


MyTimer.Initialize("MyTimer",2000)

Sub Send_Click
  t1=DateTime.Now 
  MyTimer.Enabled=True 
  Tbl.SaveTableToCSV(File.DirRootExternal,"data.csv")
End Sub



Sub MyTimer_Tick 
  Dim t2 As Long
  t2=DateTime.Now
  If t2-t1 >=20000  Then  'wait 20 sec
    MyTimer.enabled=False
    Dim ei As Email
     ei.To.Add("abc@gmail.com")
     ei.Body = "File"
     ei.Subject = "From Internal"
     ei.Attachments.Add(File.Combine(File.DirRootExternal, "data.csv"))
     StartActivity(ei.GetIntent) 
 End If
End Sub
 

incendio

Well-Known Member
Licensed User
Longtime User
I'm glad that you like this class but to be clear, the original Class was written by Erel here.
Melamoud and myself, we have added some more features.

Ok, then, also big thanks to Erel & Melamoud for this class.

Why do you save the database to a csv file ?
I need to save Table's data for further processing in desktop cpu.

Perhaps you can use a timer to allow the file to be saved. Then initiate the email afte x number of seconds expire:
Not sure about using timer, cause, no guarantee all data saved when timer expired.
Increase a numbers of second used, could cause users wait to long.

A finished event is needed for this situation.

Too bad, my skill in B4A is limited, can't modify SaveTableToCSV method to add an event.
 

LucaMs

Expert
Licensed User
Longtime User
Add an event is simple, 3 lines only.
B4X:
'Saves the table to a CSV file.
Public Sub SaveTableToCSV(Dir As String, Filename As String)
    Dim headers(NumberOfColumns) As String
    For i = 0 To headers.Length - 1
        Dim L As Label
        L = Header.GetView(i)
        headers(i) = L.Text
    Next
    StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)

    If SubExists(Callback, Event & "_CSVSaved") Then
        CallSub(Callback, Event & "_CSVSaved")
    End If
 
End Sub

This does not ensure about the completion, just the end of loop.

Perhaps it would be possible try to open the file within two blocks: do while and Try-Catch.

I try.

(sorry for my Inglese!)

[I have tried with about 24,000 records without adding more code and it seems to work]
 
Last edited:

incendio

Well-Known Member
Licensed User
Longtime User
Woo, thanks for the tip.

So, this is the syntax to create event for any Class, right?
B4X:
If SubExists(Callback, Event & "_CSVSaved") Then
   CallSub(Callback, Event & "_CSVSaved")
End If

Yeah, this does not ensure the completion, I think I will add timer for a few seconds before call this event.

Thanks again, much appreciated.
 

LucaMs

Expert
Licensed User
Longtime User


Yes, it is. Callback and Event are global variables (see Initialize sub).
http://www.b4x.com/android/forum/threads/classes-tutorial.18626/
 

LucaMs

Expert
Licensed User
Longtime User
I was thinking to test the ability to access the file. If it is possible, it means that the writing has been completed.

Something like:
B4X:
Private isAvailable as Boolean = False
Do While Not(isAvailable)
  Try
      ' open file statement
      isAvailable = True
  Catch
  End Try
  DoEvents
Loop
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
'Saves the table to a CSV file.
Public Sub SaveTableToCSV(Dir As String, Filename As String)
    Dim headers(NumberOfColumns) As String
    For i = 0 To headers.Length - 1
        Dim l As Label
        l = Header.GetView(i)
        headers(i) = l.Text
    Next
    StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)

    ' Tests the file availability - it could be an external routine.
    Private isAvailable As Boolean = False
    Private lst As List : lst.Initialize
    Do While Not(isAvailable)
      Try
          lst = File.ReadList(Dir, Filename)
          ' open file statement
          isAvailable = True
      Catch
      End Try
      DoEvents
    Loop

    If SubExists(Callback, Event & "_CSVSaved") Then
        CallSub(Callback, Event & "_CSVSaved")
    End If
End Sub
 

LucaMs

Expert
Licensed User
Longtime User


The test is not trusted.
Is it possible that the file is not locked while writing?
 

incendio

Well-Known Member
Licensed User
Longtime User
Both method, timer or checking for file, I will test it.

But for checking file, may be is not enough if test only for file access.
If file is not locked, file access could be possible while saving still in progress.

May be is better to count how many line actually saved in the file and compared this numbers with actual rows in the table.
 

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

Want to ask, what is the function of no of columns variable in sub
Table.Initialize(Me, "Tbl", 1, Gravity.CENTER_HORIZONTAL, True) ?

I always set to 1, no matter how many columns returns from sql statement. No error raised and it seem a bit faster when loading (probably this is just personal op)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…