Android Question Error on reloading Table

Declan

Well-Known Member
Licensed User
Longtime User
I am populating a table from a List Array.
B4X:
    Dim cursor1 As Cursor
    Dim txt As String
    Dim MySPList As List
    MySPList.Initialize
   
        Table1.ClearAll
        MySPList.clear
   
    txt = "SELECT [Group] AS StoreGROUP, [Store] AS STORE, [Line Total] AS LINETOTAL, [Total Sales] AS TOTALSALES,  [Budget] AS BUDGET, [Last Year] AS LASTYEAR, [GP %] AS GP, [# Of Trans] AS NUMOFTRANS, [UPT] AS UPT, [AVT] AS AVT, [Ft Count] AS FOOTCOUNT, [My Price] AS MYPRICE FROM tableSP2"
    cursor1=SQL1.ExecQuery(txt)
    For i=0 To cursor1.RowCount-1
        cursor1.Position=i
        Dim MY_GROUP As String = cursor1.GetString("StoreGROUP")
        Dim MY_STORE As String = cursor1.GetString("STORE")
        Dim MY_UPT As String = cursor1.GetString("UPT")
        Dim MY_AVT As String = cursor1.GetString("AVT")
        Dim MY_MYPRICE As String = cursor1.GetDouble("MYPRICE")
        Dim MY_NUMOFTRANS As String = cursor1.GetDouble("NUMOFTRANS")
        Dim MY_BUDGET As String =  cursor1.GetDouble("BUDGET")
        Dim MY_TOTALSALES As String = NumberFormat2(cursor1.GetDouble("TOTALSALES"), 1,2, 2,True) 
        Dim MY_LASTYEAR As String =cursor1.GetDouble("LASTYEAR")
        Dim MY_LINETOTAL As String = cursor1.GetDouble("LINETOTAL")
        Dim MY_GP As String = NumberFormat2(cursor1.GetDouble("GP"), 1,2, 2,True)
'        Dim MY_PCRATE As String = NumberFormat2(cursor1.GetDouble("PCRATE"), 1,2, 2,True)   
        Dim MY_FOOTCOUNT As String = cursor1.GetDouble("FOOTCOUNT")
       
        Dim MY_BUDGETPercent As Double = (MY_LINETOTAL / MY_BUDGET)  'PWSaleDetailLineTotal / Budget
       
        Dim MY_GROWTHPercent As Double
        If MY_LASTYEAR = 0 Then
            MY_GROWTHPercent = 0
        End If
        If MY_LASTYEAR > 0 Then
            MY_GROWTHPercent = (MY_LINETOTAL / MY_LASTYEAR) -1
        End If
        MY_GROWTHPercent = NumberFormat2(MY_GROWTHPercent,1,4,4,True)

        Dim MY_FcCONPercent As Double
        If MY_FOOTCOUNT = 0 Then
            MY_FcCONPercent = 0
        End If
        If MY_FcCONPercent > 0 Then
            MY_FcCONPercent = (MY_NUMOFTRANS / MY_FOOTCOUNT)
        End If
        MY_FcCONPercent = NumberFormat2(MY_GROWTHPercent,1,4,4,True)
        Dim MY_PRICEPercent As Double = (MY_MYPRICE / MY_LINETOTAL)  'MyPrice / PWSaleDetailLineTotal
        MY_NUMOFTRANS = NumberFormat2(MY_NUMOFTRANS,1,2,0,True)
        MY_LASTYEAR = NumberFormat2(MY_LASTYEAR,1,2,2,True)
        MY_BUDGET = NumberFormat2(MY_BUDGET,1,2,2,True)
        MY_BUDGETPercent = NumberFormat2(MY_BUDGETPercent,1,4,4,True)
        MY_MYPRICE = NumberFormat2(MY_MYPRICE,1,4,4,True)
        MY_PRICEPercent = NumberFormat2(MY_PRICEPercent,1,4,4,True)
       
        MySPList.Add(MY_GROUP &  MY_STORE &  MY_TOTALSALES  & MY_BUDGET  & MY_BUDGETPercent  & MY_LASTYEAR  & MY_GROWTHPercent &  MY_GP &   MY_NUMOFTRANS  & MY_UPT  & MY_AVT  & MY_FOOTCOUNT  & MY_FcCONPercent & MY_MYPRICE  & MY_PRICEPercent)

          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("Group", "Store", "Total Sales", "Budget", "Budget %", "Last Year", "Growth %", "GP %", "# of Trans", "UPT", "AVT", "Ft Count", "Fc Con %", "My Price", "My Price %"))
            Table1.SetColumnsWidths(Array As Int(25%x, 40%x, 25%x, 25%x, 20%x,25%x, 20%x, 15%x, 20%x, 25%x,25%x, 20%x, 20%x, 25%x, 25%x))
            Table1.AddRow(Array As String(MY_GROUP, MY_STORE, MY_TOTALSALES, MY_BUDGET, MY_BUDGETPercent, MY_LASTYEAR, MY_GROWTHPercent, MY_GP, MY_NUMOFTRANS, MY_UPT, MY_AVT, MY_FOOTCOUNT, MY_FcCONPercent, MY_MYPRICE, MY_PRICEPercent))
    Next
This works great.
BUT, when I try and reload the Table, I get the following error:
B4X:
Error occurred on line: 597 (Table)
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
    at b4a.example.table._createnewlabels(table.java:1832)
    at b4a.example.table._innerclearall(table.java:1428)
    at b4a.example.table._clearall(table.java:602)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:668)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:244)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
    at anywheresoftware.b4a.BA$3.run(BA.java:334)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5339)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **

** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
The app halts here:
B4X:
L.Typeface = cTypeFaces(i)
In Table Module:
B4X:
Private Sub CreateNewLabels As Label()
    Dim lbls(NumberOfColumns) As Label
    For i = 0 To NumberOfColumns - 1
        Dim rc As RowCol
        rc.Col = i
        Dim L As Label
        L.Initialize("Cell")
       
        L.TextSize = cTextSize
       
        If cUseColumnColors = False Then
            L.TextColor = cTextColor
        Else
            L.TextColor = cTextColors(i)
        End If
       
        If MultiTypeFace = False Then
            L.Typeface = cTypeFace
        Else
            L.Typeface = cTypeFaces(i)
        End If

        SetPadding(L, 4dip, 2dip, 4dip, 2dip)

        ' added by nir, make each label single line
        If cSingleLine Then
            Dim ref As Reflector
            ref.Target = L
            ref.RunMethod2("setSingleLine", True, "java.lang.boolean")
        End If
        L.Tag = rc
        lbls(i) = L
    Next
    Return lbls
End Sub
 

Declan

Well-Known Member
Licensed User
Longtime User
Eish, I still have this problem.
If I use remove the above code, and attempt to reload the table with a new array:
B4X:
Table1.AddRow(Array As String(MY_GROUP, MY_STORE, MY_TOTALSALES, MY_BUDGET, MY_BUDGETPercent, MY_LASTYEAR, MY_GROWTHPercent, MY_GP, MY_NUMOFTRANS, MY_UPT, MY_AVT, MY_FOOTCOUNT, MY_FcCONPercent, MY_MYPRICE, MY_PRICEPercent))
The table appends this to the existing data in the table.
I need to Clear the table before loading the new array?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you want to reload data in the Table you must clear it before !
As the name of this method Table1.AddRow says, it adds a row at the end of the Table.

As already said in THIS thread, you must move Table1.SetTypeFaces, Table1.SetHeader and Table1.SetColumnsWidths out of the For/Next loop !
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
When you want to reload data in the Table you must clear it before !
Exactly how do I clear the table?
B4X:
Table1.ClearAll
gives me the error


As already said in THIS thread, you must move Table1.SetTypeFaces, Table1.SetHeader and Table1.SetColumnsWidths out of the For/Next loop !
I have done that:
B4X:
    Dim cursor1 As Cursor
    Dim txt As String
    Dim MySPList As List
    MySPList.Initialize
   
          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("Group", "Store", "Total Sales", "Budget", "Budget %", "Last Year", "Growth %", "GP %", "# of Trans", "UPT", "AVT", "Ft Count", "Fc Con %", "My Price", "My Price %"))
            Table1.SetColumnsWidths(Array As Int(30%x, 40%x, 25%x, 25%x, 20%x,25%x, 20%x, 15%x, 20%x, 25%x,25%x, 20%x, 20%x, 25%x, 25%x))
   
    txt = "SELECT [Group] AS StoreGROUP, [Store] AS STORE, [Line Total] AS LINETOTAL, [Total Sales] AS TOTALSALES,  [Budget] AS BUDGET, [Last Year] AS LASTYEAR, [GP %] AS GP, [# Of Trans] AS NUMOFTRANS, [UPT] AS UPT, [AVT] AS AVT, [Ft Count] AS FOOTCOUNT, [My Price] AS MYPRICE FROM tableSP2"
    cursor1=SQL1.ExecQuery(txt)
    For i=0 To cursor1.RowCount-1
        cursor1.Position=i
        Dim MY_GROUP As String = cursor1.GetString("StoreGROUP")
        Dim MY_STORE As String = cursor1.GetString("STORE")
        Dim MY_UPT As String = cursor1.GetString("UPT")
        Dim MY_AVT As String = cursor1.GetString("AVT")
        Dim MY_MYPRICE As String = cursor1.GetDouble("MYPRICE")
        Dim MY_NUMOFTRANS As String = cursor1.GetDouble("NUMOFTRANS")
        Dim MY_BUDGET As String =  cursor1.GetDouble("BUDGET")
        Dim MY_TOTALSALES As String = NumberFormat2(cursor1.GetDouble("TOTALSALES"), 1,2, 2,True) 
        Dim MY_LASTYEAR As String =cursor1.GetDouble("LASTYEAR")
        Dim MY_LINETOTAL As String = cursor1.GetDouble("LINETOTAL")
        Dim MY_GP As String = NumberFormat2(cursor1.GetDouble("GP"), 1,2, 2,True)
'        Dim MY_PCRATE As String = NumberFormat2(cursor1.GetDouble("PCRATE"), 1,2, 2,True)   
        Dim MY_FOOTCOUNT As String = cursor1.GetDouble("FOOTCOUNT")
       
        Dim MY_BUDGETPercent As Double = (MY_LINETOTAL / MY_BUDGET)  'PWSaleDetailLineTotal / Budget
       
        Dim MY_GROWTHPercent As Double
        If MY_LASTYEAR = 0 Then
            MY_GROWTHPercent = 0
        End If
        If MY_LASTYEAR > 0 Then
            MY_GROWTHPercent = (MY_LINETOTAL / MY_LASTYEAR) -1
        End If
        MY_GROWTHPercent = NumberFormat2(MY_GROWTHPercent,1,4,4,True)

        Dim MY_FcCONPercent As Double
        If MY_FOOTCOUNT = 0 Then
            MY_FcCONPercent = 0
        End If
        If MY_FcCONPercent > 0 Then
            MY_FcCONPercent = (MY_NUMOFTRANS / MY_FOOTCOUNT)
        End If
        MY_FcCONPercent = (MY_FcCONPercent * 100)
        MY_FcCONPercent = NumberFormat2(MY_GROWTHPercent,1,4,4,True)
        Dim MY_PRICEPercent As Double = (MY_MYPRICE / MY_LINETOTAL)  'MyPrice / PWSaleDetailLineTotal
        MY_NUMOFTRANS = NumberFormat2(MY_NUMOFTRANS,1,2,0,True)
        MY_LASTYEAR = NumberFormat2(MY_LASTYEAR,1,2,2,True)
        MY_BUDGET = NumberFormat2(MY_BUDGET,1,2,2,True)
        MY_BUDGETPercent = (MY_BUDGETPercent * 100)
        MY_BUDGETPercent = NumberFormat2(MY_BUDGETPercent,1,4,4,True)
        MY_MYPRICE = NumberFormat2(MY_MYPRICE,1,4,4,True)
        MY_PRICEPercent = (MY_PRICEPercent * 100)
        MY_PRICEPercent = NumberFormat2(MY_PRICEPercent,1,4,4,True)
       
        MY_UPT = NumberFormat(MY_UPT,1,2)
        MY_AVT = NumberFormat(MY_AVT,1,2)
        MY_FOOTCOUNT = NumberFormat(MY_FOOTCOUNT,1,0)
       
        MySPList.Add(MY_GROUP &  MY_STORE &  MY_TOTALSALES  & MY_BUDGET  & MY_BUDGETPercent  & MY_LASTYEAR  & MY_GROWTHPercent &  MY_GP &   MY_NUMOFTRANS  & MY_UPT  & MY_AVT  & MY_FOOTCOUNT  & MY_FcCONPercent & MY_MYPRICE  & MY_PRICEPercent)
        Table1.AddRow(Array As String(MY_GROUP, MY_STORE, MY_TOTALSALES, MY_BUDGET, MY_BUDGETPercent, MY_LASTYEAR, MY_GROWTHPercent, MY_GP, MY_NUMOFTRANS, MY_UPT, MY_AVT, MY_FOOTCOUNT, MY_FcCONPercent, MY_MYPRICE, MY_PRICEPercent))
    Next
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
What error do you get ?
Wher do you clear the table ?
Without seeing the whole code it's impossible to give a concrete advice.
It would be much easier to help if you posted a small project showing the problem.

Error is:
B4X:
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at (Table):
B4X:
L.Typeface = cTypeFaces(i)
I clear the table here:
B4X:
Sub ShowSalesByStoreByRegion
    Dim cursor1 As Cursor
    Dim txt As String
    Dim MySPList As List
    MySPList.Initialize
   
    Table1.ClearAll
   
          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("Group", "Store", "Total Sales", "Budget", "Budget %", "Last Year", "Growth %", "GP %", "# of Trans", "UPT", "AVT", "Ft Count", "Fc Con %", "My Price", "My Price %"))
            Table1.SetColumnsWidths(Array As Int(30%x, 40%x, 25%x, 25%x, 20%x,25%x, 20%x, 15%x, 20%x, 25%x,25%x, 20%x, 20%x, 25%x, 25%x))
Attached is full app code (thus far)
 

Attachments

  • MyApp.zip
    106.6 KB · Views: 159
Upvote 0

mczubel

Member
Licensed User
Longtime User
go to. Table module---> Public sub TypeFaces and enabled the first function: if Typrface.length <> NumberOfcolumns then..........
In Main add tf() in Activity_Create with the other table1 setting after table1.Initialize ofcourse.
Worked perfect in samsung 3 with A 5.1.1
sorry my eng
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem is that in line 296 in the Main module:
tf = Array As Typeface(Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT, Typeface.DEFAULT_BOLD, Typeface.DEFAULT)
You set an Array of 5 TypeFaces !
But the number of columns is much bigger!
You must define a TypeFace value for each column, that means that the Array size in line 296 MUST be the same as NumberOfColumns.
 
Upvote 0
Top