B4J Question [solved] Best way to show data in a table. Must be sortable by code

DonManfred

Expert
Licensed User
Longtime User
Any suggestion on what object i should use to show some data (16 Rows) in a table like view.
I must be able to change values in the tables data cells.
And i want to be able to sort the table rows by code. Say; sort due to the values in cell 2 of each row

Any hints or tips regarding any tutorial?

Actually i´m using a TableView

B4X:
        For i = 0 To 15
            ' WorldPosition(3) As Short
            ' - CurrentLapDistance As Short
            ' - RacePosition As Byte
            ' - LapsCompleted As Byte
            ' - CurrentLap As Byte
            ' - Sector As Byte
            ' - LastSectorTime As Float
            If drivers.Name(i).Trim <> "" Then
                Dim r As Int = Rnd(0,9)
                '  table.SetColumns(Array As String("Fahrer", "Position", "CurrentLap","Compl.","Last LapTime"))
                table.Items.Add(Array As Object(akt.Get("Driver"), akt.Get("RacePosition"), akt.Get("CurrentLap"),akt.Get("LapsCompleted"),akt.Get("LastSectorTime")))
            End If
        Next

and later i do updates in this table like this

B4X:
        Dim t As Telemetry =    ParseTelemetry(raf, Packet.Data)
        'driverinfo.Text = driverinfo.Text&CRLF&"PacketType: "&t.PacketType
        'driverinfo.Text = driverinfo.Text&CRLF&"BuildVersionNumber: "&t.BuildVersionNumber
        'driverinfo.Text = driverinfo.Text&CRLF&"Speed: "&t.Speed
        Dim drlist() As ParticipantInfo = t.Driver
        For i = 0 To Min(drlist.Length-1,table.Items.Size-1)
            Dim pinfo As ParticipantInfo = drlist(i)
            If i < table.Items.Size Then
               
                Dim Row() As Object = table.Items.Get(i)
                Row(1) = pinfo.RacePosition
                Row(2) = pinfo.CurrentLap
                Row(3) = pinfo.LapsCompleted
                Row(4) = pinfo.LastSectorTime
                table.Items.Set(i,Row)
                'Log(Row(0))                   
            End If
            'driverinfo.Text = driverinfo.Text&CRLF&"CurrentLap("&i&"): "&pinfo.CurrentLap
        Next

No i want to be able to sort the table by the values of Row(1)

Any ideas?
 

stevel05

Expert
Licensed User
Longtime User
To do this sort of thing, I would usually use a list of types to store the formatted data and populate the table directly from there, you can then sort the list using SortType and repopulate the table.

Edit: It also makes it simple to apply a filter if needed.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I would usually use a list of types to
I´ve found the same solution while using the forumsearch... As i´m just using customtypes i now created a list of them and use list.sorttype method to sort the entries and then i repolulate the table data based on the sorted list...

Works fine ;-)

Thank you! :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1) sort db data directly, if you can/want
no i cant/want
I get an array of CustomTYpe live from the game. I dont want to store the data to an DB if i dont need to.
Just making a list out of them does help to use the List.SortType("RacePosition,true) method to sort the list... Fine solution for me
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
From principle

design a type
B4X:
    Type ParticipantInfo (Driver As String, WorldPosition(3) As Short, CurrentLapDistance As Short, Active As Int, RacePosition As Int, _
    LapsCompleted As Byte, CurrentLap As Byte, Sector As Byte, LastSectorTime As Float)

Dim a list, fill the list with the types
B4X:
            Dim pinfo As ParticipantInfo = drlist(i)
            driverslist.Add(pinfo)

Sort the list based on a specific key from the type and refill a tableview

B4X:
        driverslist.SortType("RacePosition",True)
        table.Items.Clear
        For i = 0 To 15
            Dim pinfo As ParticipantInfo = driverslist.Get(i)
            'Dim akt As Map = lstDriver.Get(i)
            table.Items.Add(Array As Object(pinfo.Driver, pinfo.RacePosition, pinfo.CurrentLap,pinfo.LapsCompleted,pinfo.LastSectorTime))
        Next
 
Upvote 0
Top