B4J Question Cleaning and refreshing a TableView

avalle

Active Member
Licensed User
Longtime User
Hi
I'm struggling with something that would seem rather obvious, but it does not work for me.
I have a tableview that I'm populating with data retrieved from a REST API service online. I'm polling data every 30 seconds and need to refresh the tableview accordingly.

I tried to clear the existing list of items with this:
B4X:
TableDevices.Items.Clear
and then to add the new data with this:
B4X:
TableDevices.Items.Add(row)

However, my table still contains the old rows that I wanted to clear, so I end up with duplicate data.
I think I'm missing something on how cleaning and refreshing data in a tableview works...
I'd appreciate your help!

Thanks
Andrea
 

avalle

Active Member
Licensed User
Longtime User
Here it is, but in essence the above statements are those impacting the tableview management.
B4X:
Sub Process_Globals
    Private TableDevices As TableView
    Private MyDevices As List                'List of Devices
    Type Device (Zone As String, Name As String, DevType As String, Status As Boolean, _
        Temperature As Double, Humidity As Double)
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout")     'Load the layout file
    MainForm.Show
    
    MyDevices.Initialize
    RefreshData
End Sub

Sub RefreshData ()
    Wait For (GetDevicesInfo) Complete (Result As Int)        'Get Devices Info and wait for completion
    
    LoadTable
End Sub

Sub GetDevicesInfo ()
    'Get data from the REST API webservice
    
    Do While (data > 0)
        'Add this device to the list
        Dim thisDevice As Device = CreateDevice(zoneID, zoneName, serialNo, deviceType, conValue, 0, 0, 0, "")
        MyDevices.Add(thisDevice)
    Loop
End Sub

'Create a Device object
Public Sub CreateDevice (Zone As String, SerialNo As String, DevType As String, Status As Boolean, Temperature As Double, Humidity As Double) As Device
    Dim dev As Device
    dev.Initialize
    dev.Zone = Zone
    dev.SerialNo = SerialNo
    dev.DevType = DevType
    dev.Status = Status
    dev.Temperature = Temperature
    dev.Humidity = Humidity
    Return dev
End Sub

'Load values into the table
Sub LoadTable ()
    Dim temp As String
    Dim humid As String
    
    'Clear the table if data exists
    TableDevices.Items.Clear
    
    'Load values
    For Each devs As Device In MyDevices
        If (devs.Status) Then
            temp = NumberFormat2(devs.Temperature, 1, 2, 2, False) & "°C  "
            humid = NumberFormat2(devs.Humidity, 1, 1, 1, False) & "%  "
        Else
            temp = "---"
            humid = "---"
        End If
        Dim row() As Object = Array (devs.Zone, devs.SerialNo, devs.DevType, devs.Status, temp, humid)
        TableDevices.Items.Add(row)
    Next
End Sub
 
Upvote 0

sdleidel

Active Member
Licensed User
Longtime User
Ok...
I think mydevices is a List...
Do you clear this List after you get new Data from your api ?

otherwise you append new Data to your old Data.
 
Upvote 0

avalle

Active Member
Licensed User
Longtime User
Actually I need to clear the MyDevice list before getting new data.
That was the problem...
Thanks for helping out!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…