Android Question UDP to Table

davelew1s

Active Member
Licensed User
Longtime User
Hi! I am using Erel's Table class as a test bed for my next app, the Table class is unchanged but i've commented out some line and inserted amy own in the Main act ... this is the 'main' code:-
B4X:
Sub Process_Globals
   Dim UDPSocket1 As UDPSocket   
   Dim SF As StringFunctions
   
End Sub

Sub Globals
   Dim Table1 As Table     ', Table2 As Table
   Dim Label1 As Label
   Dim data As String
   Dim EditText1 As EditText
   Dim count As Int = 0
   Dim Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity .LoadLayout ("Layout1")
   If FirstTime Then
  UDPSocket1.Initialize("UDP",9742, 8000)
  End If



   Table1.Initialize(Me, "Table1", 5)
   Table1.AddToActivity(Activity, 0, 0dip, 100%x, 70%y)   
   Table1.SetHeader(Array As String("Icao", "Flight", "Lat", "Lon","Active"))
   Table1.SetColumnsWidths(Array As Int(150dip, 150dip, 150dip, 150dip, 150dip))   '100%x - 260dip))
   'Activity.AddMenuItem("Jump To 3000", "Jump1")
   'Activity.AddMenuItem("Jump To 0", "Jump2")
   't.Initialize ("active",60000)
   't.Enabled = True
   'Table2.Initialize(Me, "Table2", 0)
   'Table2.AddToActivity(Activity, 0, 55%y, 100%x, 45%y)
   'Table2.LoadTableFromCSV(File.DirAssets, "citylist.csv", True)
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
Dim row() As String
count = count + 1
Label1 .Text = count   'Number of packet received jus for testing


   data = BytesToString(Packet.data, Packet.Offset, Packet.Length, "UTF8")
' this is a sample of the data   
'SB"2013/11/09","15:16:50.000","1111111","40643C","EZY48TH ","Unknown","0","35000","35000","46.68608"," -4.99476","0","0","511.0","196.5","0","0000"
   
'DoEvents
   EditText1.Text =data     'To watch data coming in
   data = data.SubString (4)   'Removes SB
   Dim icao As String =SF.SplitGetWord(data, ",",4)
   Dim flight As String =SF.SplitGetWord(data, ",",5)
   Dim lat As String =SF.SplitGetWord(data, ",",10)
   Dim lon As String =SF.SplitGetWord(data, ",",11)
   
   icao = SF.Mid (icao,2,6)
   flight = SF.Mid (flight,2,7)
   lat = SF.Mid (lat,2,8)
   lon = SF.Mid (lon,2,9)

'DoEvents   
   Label2 .Text = Table1.size   'Number of rows
   For i = 0 To    Table1.size - 1
   Dim cell As String =  Table1.GetValue (0 , i )
     If cell = icao Then    'Already in table update lat and lon
       Table1.SetValue (2 , i , lat )
       Table1.SetValue (3 , i , lon )
       Table1.SetValue (4 , i , "A" )
       Return
     End If
   Next
   
   
'DoEvents
   row = Array As String (icao,flight,lat,lon,"A")
   Table1.AddRow(row)
   
End Sub

My problem is the data is an almost continuous stream so while receiving i cannot scroll the table or do anything else.I've tried 'Doevents' in varios places but this caused the app to go slower and slower until it stopped, I thought of using the Threading library but got nowwhere, i found a post which said there are simpler and better ways than threading...thats what i'm looking for. Any help or suggestions would be greatly appreciated
 

davelew1s

Active Member
Licensed User
Longtime User
Hi Erel!
Thanks for your reply i was testing it in debug...release mode is alot better but stilll a little jerky. The table size varies but 9 columns and 20 rows is average. If you have any further suggestions i will give them a try.
Thanks Dave.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I guess that the slow part is updating the table. Creating another thread will not help as you cannot access the UI from other threads.

You can optimize your code. For example you can use a Map to quickly find the row that needs to be updated. However you should first try to understand what slows down your app.

Note that the packet is received by a background thread. Only when it is ready the event is raised.
 
Upvote 0
Top