Android Question TableView error due to dynamic Column Count and SetCellAlignments

Anser

Well-Known Member
Licensed User
Hi,

In my Activity I have 2 views
Spinner and TableView

I have used designer to create the views

My requirement:-

The user uses the Spinner to select a Table Name and the TableView will display the contents from the selected table by the user.

Problem:-

Table No 1 contains 6 columns
Table No 2 contains 3 columns

If the user selects the table no 2 first and then the second table then it errors out. I guess that the reason is the columns in Table 2 is lesser than the Table 1

If the user selects the Table No 1 first (which has more number of columns) and then the Table no 2, it works fine ie no errors.


What would be the right way to get this done. Should I keep 2 different TableViews ie one for each. ?

The run time error that I get is given below


** Activity (viewmasters) Resume **
table_innerclearall (java line: 1342)
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at com.mycompany.myappname.table._innerclearall(table.java:1342)
at com.mycompany.myappname.table._loadsqlitedb(table.java:1565)
at com.mycompany.myappname.viewmasters._spnrmasters_itemclick(viewmasters.java:454)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.BA$1.run(BA.java:303)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


B4X:
TblView.ClearAll
TblView.LoadSQLiteDB(Starter.SQL1, cSql, True)
TblView.SetCellAlignments(Alignments)
Any help will be appreciated.

EDIT:- I noticed that this issue occurs only if I use TblView.SetCellAlignments
 
Last edited:

Anser

Well-Known Member
Licensed User
What is the content of Alignments?
How do you update the TabeView with the new Table?
This is the code that I use
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#Extends: android.support.v7.app.AppCompatActivity

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ActionBar As ACToolBarLight
    Private SpnrTables As Spinner
    Private TblView As Table
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("ViewTables")

    SpnrTables.AddAll(Array As String("Choose Table","Table 1", "Table 2"))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SpnrTables_ItemClick (Position As Int, Value As Object)
    Dim cTableType As String
    cTableType = Value

    Dim cSql As String

    If cTableType = "Table 1" Then ' Table has 6 columns
    
        cSql = $"SELECT Col_1, Col_2, Col_3, Col_4,
                        Col_5, Col_6
                 FROM Table1"$
                     
        Dim Alignments() As Int
        Alignments = Array As Int(Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.CENTER, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.CENTER, Gravity.CENTER_VERTICAL) )

    Else If  cTableType = "Table 2" Then ' Only 3 Columns
    
        cSql = $"SELECT Col_1, Col_2, Col_3
                 FROM Table2 "$

                     
        Dim Alignments() As Int
        Alignments = Array As Int(Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL), _
                                  Bit.Or(Gravity.CENTER, Gravity.CENTER_VERTICAL) )


    End If

    If cTableType <> "Choose Table" Then

        TblView.ClearAll
        TblView.LoadSQLiteDB(Starter.SQL1, cSql, True)
        'If the following line is used then the error
        'TblView.SetCellAlignments(Alignments)
    
    End If

End Sub
 

klaus

Expert
Licensed User
Your code looks OK to me.
Which version of TableView are you using?
The latest version is 2.16.

For testing, put Log(TblView.NumberOfColumns) before and after TblView.SetCellAlignments(Alignments).
B4X:
Log(TblView.NumberOfColumns)
TblView.SetCellAlignments(Alignments)
Log(TblView.NumberOfColumns)
You can remove TblView.ClearAll because TblView.LoadSQLiteDB does it internally.

Or can you post your project or a smaller one showing the problem so I could have a closer look at it and test it.
In the attached test project for the Table class, adding TblView.SetCellAlignments(Alignments) works.
 

Attachments

Anser

Well-Known Member
Licensed User
Which version of TableView are you using?
I was using 2.15, now updated to 2.16 from your post. Unfortunately, I get the same error.

You can remove TblView.ClearAll because TblView.LoadSQLiteDB does it internally.
When the error occured I added this line, just to find out whether TblView.ClearAll would clear the error or not

For testing, put Log(TblView.NumberOfColumns) before and after TblView.SetCellAlignments(Alignments).
It is showing the correct column numbers.

Here is a self contained sample to reproduce the error that I described. From the Spinner, choose "Table 2" first and then "Table 1"

One more problem which I have noticed is that TableView creates a run time error, if the sort_asc.png and sort_desc png is not included in the files for the project. It expects these 2 files in the project.
 

Attachments

klaus

Expert
Licensed User
I get the error too.
There was an error in the innerClearAll routine when using multi cell alignment.
Attached a modified version with version 2.17 of the Table class.
 

Attachments

Top