Android Question Questions on B4XTable

vmag

Active Member
Hello there!
1. How to programmatically go to (highlight) a specific row? In TableView, this could be done like this:
B4X:
TableView1.Selectedrows=i
2. How can I set the percentage width of columns in B4XTable? In TableView this could be done like this:
B4X:
Dim w As Int = Round(TableView1.Width)  ' 100 %
TableView1.SetColumns(cols)                 
TableView1.SetColumnWidth(0, 0)         ' 0 %     
TableView1.SetColumnWidth(1, w*80/100)  ' 80 %           
TableView1.SetColumnWidth(2, w*15/100)  ' 15 %
 

Mahares

Expert
Licensed User
Longtime User
1. How to programmatically go to (highlight) a specific row? I
This extension to B4XTable allows you to select a single line, multiple lines, single cell, etc. There is an example that makes it easy to implement:
https://www.b4x.com/android/forum/t...-selection-modes-for-b4xtable.114294/#content
2. How can I set the percentage width of columns in B4XTable? In TableView this could be done like this:
B4X:
B4X:
B4XTable1.AddColumn("US County", B4XTable1.COLUMN_TYPE_TEXT).Width = 5%x
    B4XTable1.AddColumn("Name", B4XTable1.COLUMN_TYPE_TEXT).Width=45%x
    B4XTable1.AddColumn("State", B4XTable1.COLUMN_TYPE_TEXT).Width =35%x
    B4XTable1.AddColumn("Number Example", B4XTable1.COLUMN_TYPE_NUMBERS).width=15%x
You can also use dips. You can also iterate through the columns:
B4X:
For Each c As B4XTableColumn In B4XTable1.Columns
        c.Width=15%x
        If c.Id="Name" Then c.Width=55%x
'        c.Width=165dip 'you can use dips also
    Next
 
Last edited:
Upvote 0

vmag

Active Member
Thanks for the answer!
With point 2, everything is fine, it works perfectly.
Point 1 I just did according to Your example, I needed to make the selection of the entire line and I implemented it through SINGLE_LINE_PERMANENT, but I still have another problem - I need to implement such a mechanism:
1. I know that the table does not contain the name "Peter".
2. In another procedure, I add the name "Peter" to the table and know that its key = 134.
3. Now I need to update the B4XTable in form and select the row with the name "Peter".
The instructions say that you need to apply SelectedLines, I know that this is line = 5, I write
B4X:
XSelections.SelectedLines.Put(5,"")
Tbl2.Refresh
, but the line is not selected
I even just try to programmatically select row number 5 in the table after opening the form, and it doesn't work for me... That is, my hands are allocated rows normally, but I do not know what to insert after the operator Tbl2. SetData(List 1) so that after opening the form in the table was allocated row № 5
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I even just try to programmatically select row number 5 in the table after opening the form, and it doesn't work for me
You are asking if you know the rowid of the row , in this example 5, you want the row to be automatically selected (hilighted). Here is the code for that:
B4X:
B4XTable1.SetData(Data)
    XSelections.Initialize(B4XTable1)
    XSelections.Mode = XSelections.MODE_SINGLE_LINE_PERMANENT

    Dim rs As ResultSet = B4XTable1.sql1.ExecQuery2("SELECT rowid from data WHERE rowid  =?", Array As String(5))
    Do While rs.NextRow
        XSelections.SelectedLines.Put(rs.GetLong("rowid"), "")
    Loop
    rs.Close
    XSelections.Refresh
 
Upvote 0

vmag

Active Member
Thank you, in this wrapper XSelections .SelectedLines.Put does work, although I was hoping for a slightly different effect... In this case, if the desired line is not located on the first page, it is highlighted without automatically going to this page - the visual effect loses its meaning. Naturally, this is not a question for you personally, I'm just testing the capabilities of b4x compared to other platforms
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
In this case, if the desired line is not located on the first page, it is highlighted without automatically going to this page
If you want to go directly to the hilighted selection after you programmatically make your selection, say your rowid selected is 20, you can create a data view like this and it will automatically go to it. There may be other ways.
B4X:
Dim xrowid As Int = 20
 B4XTable1.CreateDataView($"rowid = ${xrowid}"$) 'you need to clear the data view when done with it
 
Upvote 0

vmag

Active Member
Thank you for the option, it works, but I would like the table not to be cleared to one record, but just to open on the page with record number 20... Is this possible ?
I made a transition to the right page with the right line so far so
B4X:
 Tbl2.CurrentPage=i/8+1
because I have 8 records on the page and for example if the record is numbered 20, then the page = 20/8 +1 = 2+1= 3, but it seems to me that this is a bit stupid, because I do not know how many entries on the B4XTable page will be next time
Actually there are two questions with this:
1. Can I force the number of rows per page?
2. Can I find out how many rows are placed on the page after
B4X:
B4XTable1.SetData (cols)
Well, along the way, another third question is how to change the alignment of text in columns, it is always centered and does not even depend on what is set in the constructor
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
1. Can I force the number of rows per page?
Yes. You can set: I usually do it after I set up the data types for the columns:
B4X:
B4XTable1.MaximumRowsPerPage=11   'it will not work correctly if you use the search box
2. Can I find out how many rows are placed on the page after
You can use something like this, but you can tweak to jump to the given page knowing what rowid you want to go to:
B4X:
Sub btn_Click
    If B4XTable1.mCurrentCount > 0 Then
        Dim NumberOfPages As Double = xrowid/B4XTable1.MaximumRowsPerPage  'xrowid is the rowid you want to go to declared in Globals, exple:20
        Log("pages" & NumberOfPages)
    End If
    If NumberOfPages Mod B4XTable1.MaximumRowsPerPage > 0 Then
        B4XTable1.CurrentPage = Ceil(NumberOfPages)
        Log(Ceil(NumberOfPages))
    Else
        B4XTable1.CurrentPage = Floor(NumberOfPages)
        Floor(NumberOfPages)
    End If
End Sub
how to change the alignment of text in columns
B4X:
B4XTable1.MaximumRowsPerPage=11
    B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
    For Each col As B4XTableColumn In B4XTable1.Columns
        For i = 1 To col.CellsLayouts.Size - 1        'starts at 1 due to header
            Dim pnl As B4XView = col.CellsLayouts.Get(i)
            pnl.GetView(0).SetTextAlignment("CENTER", "LEFT")
        Next
    Next
 
Upvote 0

vmag

Active Member
Thank you for your answer, but I probably won't be able to use it... Unfortunately, MaximumRowsPerPage sets the upper limit on the number of rows, and if the value is 8, I got 8 rows per page on the first device, and 6 on the second device, respectively, the algorithm will not work correctly on the second device... I didn't succeed with text alignment either.I tried it both before and after filling in the table. it doesn't work the same way as after selecting values in the constructor. Maybe my hands are sticking out on the wrong side, all sorts of things happen... But in Tableview I didn't have any particular problems at all
 
Upvote 0

vmag

Active Member
By the way, I found that using Maximum Rows Per Page still affects Your algorithm for selecting a table row by its number. Your algorithm just stopped working. But I still got out of the situation... I needed to highlight a row when adding a new record so that the user could see the result of adding the record. Now I (only when adding) change the sorting in descending order of the key / counter and the new entry is now always the first and it is very easy to select it. Thank You for participating in my problems.
 
Upvote 0
Top