Android Question [Solved]How to sort B4XTable based on a column

Mikelgiles

Active Member
Licensed User
Column type set to DATE or TEXT. I did a search on "sort B4XTable" but I am finding a lot of B4XTable without sort so I am guessing that I must be searching wrong.
 

mangojack

Well-Known Member
Licensed User
from the B4XTable Example ...

Customization

Many things can be customized. Will start with a few simple things.
B4XTable.AddColumn returns a B4XTableColumn type instance. You can modify this type to change the column behavior:
B4X:
Dim StateColumn As B4XTableColumn = B4XTable1.AddColumn("State", B4XTable1.COLUMN_TYPE_TEXT)
StateColumn.Width = 80dip
StateColumn.Sortable = False  '*****
By default all columns are sortable and text columns are searchable.


Have you tried clicking on the column header cell ?
 

Mikelgiles

Active Member
Licensed User
Customization

Many things can be customized. Will start with a few simple things.
B4XTable.AddColumn returns a B4XTableColumn type instance. You can modify this type to change the column behavior:
B4X:
Dim StateColumn As B4XTableColumn = B4XTable1.AddColumn("State", B4XTable1.COLUMN_TYPE_TEXT)
StateColumn.Width = 80dip
StateColumn.Sortable = False  '*****
By default all columns are sortable and text columns are searchable.


Have you tried clicking on the column header cell ?
https://www.b4x.com/android/forum/threads/b4x-b4xtable-cross-platform-sortable-searchable-customizable-table.102322/

Yes, clicking the column headers work just fine but I need to do it by code instead.
I did find this code
B4X:
B4XTable1.AddColumn("Name", B4XTable1.COLUMN_TYPE_TEXT).InternalSortMode = "DESC"
in
https://www.b4x.com/android/forum/threads/sort-b4xtable.103352/#post-647712
which I changed to
B4X:
CW=B4XTableM.AddColumn("paydate", B4XTableM.COLUMN_TYPE_DATE).InternalSortMode = "ASC"
to put in my app. This code looked like it would work, but I get error

"Error B4J line: 349
CW=B4XTableM.AddColumn(\src\b4j\example\main.java:327: error: incompatible types: boolean cannot be converted to _b4xtablecolumn
cw = (b4j.example.b4xtable._b4xtablecolumn)((_b4xtablem._addcolumn /*b4j.example.b4xtable._b4xtablecolumn*/ (null,"paydate",_b4xtablem._column_type_date /*int*/ ).InternalSortMode /*String*/ ).equals("ASC"));

the code I copied was for B4A and I am using B4J but I thought it would work on both. And the sample code was using column_type_TEXT and I need it for column_type_DATE. Before adding .InternalSortMode = "ASC" the code was working ok so I assume that I am using the added code incorrectly.
 

emexes

Well-Known Member
Licensed User
clicking the column headers work just fine but I need to do it by code instead.
This is a bit out of left-field, perhaps not even possible, but: maybe there is some way of generating touch/mouse events so that your code can click the column header(s)? Or of generating a _Click call to the column header?
 

mangojack

Well-Known Member
Licensed User
Yes, clicking the column headers work just fine but I need to do it by code instead.
After re-reading your question .. appologies. Try this

B4X:
Sub Button1_Click  
    Dim column As B4XTableColumn = B4XTable1.GetColumn("Name")
    If column.InternalSortMode = "ASC" Then
        column.InternalSortMode= "DESC"
    Else
        column.InternalSortMode= "ASC"
    End If
  
    B4XTable1.Refresh  
End Sub
 
Last edited:

Erel

Administrator
Staff member
Licensed User
I think being B4J increases your chances of success, although you might need to look on the Java side of the B4J/Java fence.
How is Java related here? B4XTable is written in B4X.
 

emexes

Well-Known Member
Licensed User
How is Java related here? B4XTable is written in B4X.
I was thinking in terms of generating UI events as generically as possible, and figured that would be via Java and thus there would be heaps of Stack Exchange et al discussion about it. For some reason that I can't remember now, I didn't think that B4XTable source code was available. When I get home, I'll delete that spurious post of mine.
 

Mikelgiles

Active Member
Licensed User
After re-reading your question .. appologies. Try this

B4X:
Sub Button1_Click 
    Dim column As B4XTableColumn = B4XTable1.GetColumn("Name")
    If column.InternalSortMode = "ASC" Then
        column.InternalSortMode= "DESC"
    Else
        column.InternalSortMode= "ASC"
    End If
 
    B4XTable1.Refresh 
End Sub
Thank you mangojack. It works perfectly and is a better fit for what I am doing than what I was attempting so I dont even care why my code would not work!.

Thanks again!
 
Top