Android Question Table View fill from list

Declan

Well-Known Member
Licensed User
Longtime User
I am using the following code to fill a TableView from a List:
B4X:
Sub ShowSalesByStoreByRegion
    Dim cursor1 As Cursor
    Dim txt As String
    Dim MySPList As List
    MySPList.Initialize
   
        Table1.ClearAll
   
    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 = (MY_LINETOTAL / MY_LASTYEAR) -1 '(PWSaleDetailLineTotal / LastYear)-1
        Dim MY_FcCONPercent As Double = (MY_NUMOFTRANS / MY_FOOTCOUNT) 'NumerofTrans / Foot Counts
        Dim MY_PRICEPercent As Double = (MY_MYPRICE / MY_LINETOTAL)  'MyPrice / PWSaleDetailLineTotal
       
'        MySPList.Add(MY_GROUP & TAB & MY_STORE & TAB & MY_TOTALSALES & TAB & MY_BUDGET & TAB & MY_BUDGETPercent & TAB & MY_LASTYEAR & TAB & MY_GROWTHPercent & TAB & MY_GP & TAB &  MY_NUMOFTRANS & TAB & MY_UPT  & TAB & MY_AVT & TAB & MY_FOOTCOUNT & TAB & MY_FcCONPercent & TAB & MY_MYPRICE & TAB & MY_PRICEPercent)
        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, 25%x, 25%x, 25%x, 25%x,25%x, 25%x, 25%x, 25%x, 25%x,25%x, 25%x, 25%x, 25%x, 25%x))
            For i=0 To MySPList.Size -1
                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
   
    Next
    Table1.Visible = True
End Sub

The problems I have are:
The Table does not clear, but appends data
How am I able to increase the Height of the Table?
 

klaus

Expert
Licensed User
Longtime User
You are using the Table1 class the wrong way.
You should set Table1.SetTypeFaces, Table1.SetHeader and Table1.SetColumnsWidths only once, not in the For/Next loop !
You must replace :
B4X:
For i=0 To MySPList.Size -1
    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
simply by:
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))
Another problem:
You have a For/Next loop in another For/Next loop with the same variable i !!!
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Table1.AddRow(ArrayAsString(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))
I now need to total certain columns and append these records to the bottom of the Table
Something like:
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))

Table1.AddRow(Array As String("Totals:", MY_TOTALSALES, MY_MYPRICE))
Is it possible to have "& TAB &" that will enable me to get the totals to display in the correct column?
For instance: "Totals:" will be in the first column (col(0)), "MY_TOTALSALES" will be in the third column (col(2))
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You might want to reserve some space below table1 and create a second table on a panel created by code. Just to give you an idea something like this and you can adjust the placement of the columns and table. Perhaps, @klaus has a better idea:
B4X:
Dim p2 As Panel   'in globals

    p2.Initialize("")
    Activity.AddView(p2, 20dip, 100%y-60dip, 100%x - 40dip, 60dip)
    Table2.Initialize(Me,"table2")
    Table2.InitializeTable(2,Gravity.CENTER_HORIZONTAL, True)
    Table2.AddToActivity(p2, 0, 0, p2.Width, p2.Height)
    Dim MyQuery As String ="SELECT 'TOTALS:' as TOT, sum(MY_TOTALSALES) as TS FROM tableSP2"
    Table2.LoadSQLiteDB(SQL1,MyQuery,True)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
For your totals you can enter a row like you tried but you must enter an empty string in the columns wittout a value like:
B4X:
Table1.AddRow(Array As String("Totals:", "", MY_TOTALSALES, "", "", MY_MYPRICE, "", "", ""....))
The number of items in the Array must be equal to the number of columns.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
@Mahares & @klaus
Thanks guys - I went with Klaus's recommendation
Found it easier to assemble the data in the loop, then display the array in the list instead of multiple SQL queries (due to the data I need)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…