Android Question B4X Table, add sub total?

emexes

Well-Known Member
Licensed User
Is it possible to add sub total to B4X table? Any idea how to do it?
I have not used B4XTable... just throwing this idea into the mix, maybe it will seed an epiphany :)

(I am interpreting "sub total" as "total", ie the total of every row in a column, and not sub-groups of rows)

Looking at the code in this post:

https://www.b4x.com/android/forum/threads/b4x-b4xtable-with-custom-cells-layout.102352/

I imagine that instead of just having one header row (visible row 0) you could have two header rows, and make one of them the total(s) line. Or if you are able to access the header row panels/layouts/views then perhaps you could add the totals to that existing single row, eg:

upload_2019-8-20_11-46-8.png
 

emexes

Well-Known Member
Licensed User
In fact, the above sample has colored-text headings, which suggests it might be using (or be capable of using) CharSequence/CSBuilder or similar, in which case, you could just put the totals into the heading text, without having to muck about with panels/layouts/views.

I know that traditionally totals go at the bottom, but I have found that once you let go of that convention, totals at the top is far more practical.

If you can intercept a click or long-click on the header, or you can place a button in it somewhere (pretty sure CharSequence can handle clickable text/emojis) then you could alternate the headers to be with/without totals.
 

emexes

Well-Known Member
Licensed User
No, I mean sub total, not total, see post # 6.
Oh well, that's the sneak-it-into-the-heading method shot, then.

If the table is small enough eg hundreds of rows, not millions, then probably simplest just to copy all records across from DB and, as part of this copy process, add subtotal and grand total lines after each group and at end.

But if you do that, you cannot re-sort the table on different columns - it would have to stay in the order that it was copied from/in/as/to.

Something like:
B4X:
groupcode = ""    'or any other code guaranteed not to exist in database
groupcount = 0
grouptotal = 0
grandtotal = 0

for each record in database in groupcode order    'translate this pseudocode into SQL
    if record.groupcode <> groupcode then
        if groupcount <> 0 then
            add subtotal row for groupcode grouptotal
        end if

        groupcode = record.groupcode
        groupcount = 0
        grouptotal = 0
    end if

    add row for record.groupcode  record.groupquantity

    increment groupcount
    add record.quantity to grouptotal
    add record.quantity to grandtotal
next

if groupcount <> 0 then
    add subtotal row for groupcode grouptotal
end if

add grand total row for grandtotal
 

incendio

Well-Known Member
Licensed User
Oh well, that's the sneak-it-into-the-heading method shot, then.

If the table is small enough eg hundreds of rows, not millions, then probably simplest just to copy all records across from DB and, as part of this copy process, add subtotal and grand total lines after each group and at end.

But if you do that, you cannot re-sort the table on different columns - it would have to stay in the order that it was copied from/in/as/to.

Something like:
B4X:
groupcode = ""    'or any other code guaranteed not to exist in database
groupcount = 0
grouptotal = 0
grandtotal = 0

for each record in database in groupcode order    'translate this pseudocode into SQL
    if record.groupcode <> groupcode then
        if groupcount <> 0 then
            add subtotal row for groupcode grouptotal
        end if

        groupcode = record.groupcode
        groupcount = 0
        grouptotal = 0
    end if

    add row for record.groupcode  record.groupquantity

    increment groupcount
    add record.quantity to grouptotal
    add record.quantity to grandtotal
next

if groupcount <> 0 then
    add subtotal row for groupcode grouptotal
end if

add grand total row for grandtotal
Not sure that B4XTable is the best control for this use case. It would be simpler to use xCLV.
Thanks all for your replied.

If only for B4A, I could UltimateListView (ULV) for sub total. But ULV not cross platform. Hope in the future, B4X Table can develop this feature or I will try emexes to see if it can works.
 

udg

Expert
Licensed User
xCLV basically is a scrolling list of panels.
You load the layout you need on each panel (in your case they will all look alike).
You could even add items to those panels in code and manage them and their content.

Edit: Erel was faster than me by a second or two :)
 

incendio

Well-Known Member
Licensed User
Ok guys, thanks for the hints, will look into xClv then.
 
Last edited:
Top