Android Question [SOLVED]How to get the Last Record in a BXTable ?

Daniel44

Active Member
Licensed User
Hi everyone!

I don't know if it is possible ... When the Activity initializes I need my BXTable "goes to" the last Row. I know you'll say "Hey change the sql sentence ex. "ordeb by Id desc". but I'd like avoiding that since when I have to edit by clicking on the table the RowIds make a mess.

So as I said I don't know if it's possible do that.

In order to show my records I'm using this code

B4X:
Sub CargarGridCampanias
    ProgressDialogShow("Recurperando Campañas desde el Server")
    ExecuteRemoteQuery("SELECT * FROM campania",GRIDCAMPANIAS)
End Sub

Then my JobDone

B4X:
Sub JobDone(Job As HttpJob)
 
        Select Job.JobName
           
            Case GRIDCAMPANIAS
                Dim ListaCampanias As List
                ListaCampanias.Initialize
                ListaCampanias = parser.NextArray
                For i = 0 To ListaCampanias.Size -1
'                    Dim MapaListaCampania As Map
'                    MapaListaCampania = ListaCampanias.Get(i)
                    Log(ListaCampanias.Get(i))
                Next
               
                BXTBLCAMP.AllowSmallRowHeightModifications= False
               
               
                BXTBLCAMP.AddColumn("ID", BXTBLCAMP.COLUMN_TYPE_NUMBERS).Width = 30dip
                BXTBLCAMP.AddColumn("NOMBRE CAMPAÑA", BXTBLCAMP.COLUMN_TYPE_TEXT).Width = 200dip
                BXTBLCAMP.AddColumn("ESTADO",BXTBLCAMP.COLUMN_TYPE_TEXT).Width=130dip
               
               
                Dim listaCamp As List
                listaCamp.Initialize
               
                For Each minimap As Map In ListaCampanias
                    Dim id As Int = minimap.Get("ID")
                    Dim nombre As String = minimap.Get("NOMBRE")
                    Dim estado As String = minimap.Get("ESTADO")
                   
                   
                    Dim renglon(3) As Object
                    renglon(0) = id
                    renglon(1) = nombre
                    renglon(2) = estado
                   
                    listaCamp.Add(renglon)
                Next
                BXTBLCAMP.SetData(listaCamp)

It is working .. but my Bxtable is small and I just can see 2 records so when I insert a third record I must use the ">>" botton to go the last record.
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
Does the B4XTable not have a .ScrollTo or .GoToLast method, or something similar? If running an ORDER BY Id DESC query screws up your table when you go to edit it, then I'd say you're not loading it correctly.

- Colin.
 
Upvote 0

Daniel44

Active Member
Licensed User
 
Upvote 0

Daniel44

Active Member
Licensed User
Find the number of pages: https://www.b4x.com/android/forum/threads/how-get-page-counts-in-b4xtable.108487/post-678284 (not available if there is a filter)
Switch to: B4XTable.CurrentPage = NumberOfPages - 1

Hey Erel thank you for answering... well I've done what you've indicated
Here's my code on Bxtable dataUpdated

B4X:
Sub BXTBLCAMP_DataUpdated
    btnNext.Initialize("bt1nex")
    btnNext.Enabled = BXTBLCAMP.lblNext.Tag
    btnPrev.Initialize("bt1prev")
    btnPrev.Enabled = BXTBLCAMP.lblBack.Tag
    btnFirst.Initialize("btnF")
    btnFirst.Enabled = BXTBLCAMP.lblFirst.Tag
    btnLast.Initialize("btnL")
    btnLast.Enabled = BXTBLCAMP.lblLast.Tag
    If BXTBLCAMP.mCurrentCount > 0 Then
    Dim NumerodePaginas As Int = Ceil(BXTBLCAMP.mCurrentCount / BXTBLCAMP.VisibleRowIds.Size)
    'Log(NumerodePaginas)
        BXTBLCAMP.CurrentPage = NumerodePaginas -0
    end if
End Sub

It's working, it goes to the last page but.....Now If I wish to get back to first page It doesn't let me do that

I've tried this:

B4X:
If BXTBLCAMP.mCurrentCount > 0 Then
    Dim NumerodePaginas As Int = Ceil(BXTBLCAMP.mCurrentCount / BXTBLCAMP.VisibleRowIds.Size)
    'Log(NumerodePaginas)
        BXTBLCAMP.CurrentPage = NumerodePaginas -0
    
    else if BXTBLCAMP.lblFirst Then
         BXTBLCAMP.CurrentPage = NumerodePaginas -1
     End If
     end Sub

but it didn't work either. I was thinking to null that code when I touch the btnFirst but I don't how to do it. Thank you so much.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is wrong:
B4X:
  btnNext.Initialize("bt1nex")
    btnNext.Enabled = BXTBLCAMP.lblNext.Tag
    btnPrev.Initialize("bt1prev")
    btnPrev.Enabled = BXTBLCAMP.lblBack.Tag
    btnFirst.Initialize("btnF")
    btnFirst.Enabled = BXTBLCAMP.lblFirst.Tag
    btnLast.Initialize("btnL")
    btnLast.Enabled = BXTBLCAMP.lblLast.Tag

Why are you initializing these buttons in DataUpdated? DataUpdated is called many times.

It's working, it goes to the last page but.....Now If I wish to get back to first page It doesn't let me do that
Same as above. DataUpdated will be called many times and especially when you switch pages. Your code will revert to the same page each time.

There will be a better solution in the next version of B4XTable however for now you should use:
B4X:
  BXTBLCAMP.SetData(listaCamp)
Sleep(100)
'Set the last page here.
 
Upvote 0

Daniel44

Active Member
Licensed User
Working now!! thank you so much Erel
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…