Android Question How iterate over a list of single arrays.

IslandMedic

Member
Licensed User
Longtime User
I am trying to build a spinner with items. the items are in a list.

(ArrayList) [[1, Brad brown, brad, PCP-IV], [2, Penny brown, penny, FR]]

When I run this code:
B4X:
Sub build_user_spinner
    Log("build spinner staff")
    Dim mylist As List
    mylist.Initialize
   
    Dim arr() As String
   
    mylist = File.readList(File.DirInternal,"stafflist.txt")
    ' set first value as action statement   
    Spinner1.Add("Select User")
    Spinner2.Add("Select User")
   
    For i = 0 To mylist.Size -1
        arr = mylist.Get(i)
        Log(arr)
       
       
        Spinner1.Add(mylist.Get(1))
        Spinner2.Add(mylist.Get(1))
   
    Next   
     
End Sub

in my logs I get:

[1, Brad brown, brad, PCP-IV]
[2, Penny brown, penny, FR]

this is the first step, now I need to access each piece of the data in the string to build a nice looking spinner.
i.e. Brad Brown (PCP-IV)

To me this arr looks like an array, so is that how I access the items?

thanks
Brad
 

DonManfred

Expert
Licensed User
Longtime User
File.ReadList returns a list where every item is a string.
but the lines seems to hold an array? This code give
[1, Brad brown, brad, PCP-IV]
[2, Penny brown, penny, FR]

in the logs... As he dont get an error i guess the line IS an array
B4X:
Dim arr() As String
 
Upvote 0

IslandMedic

Member
Licensed User
Longtime User
ok so this data comes from a websocket server. this is the code on the server that gets the data from mysql and then sends it over the wire.
B4X:
Public Sub Get_Staff(mm As Map)
    Log("get staff")
    
    Dim sq As SQL = Main.pool.GetConnection
    Dim query As String   
    Dim cursor As ResultSet
    Dim data As List
    data.Initialize
   
    'get the staff list that is sched for today
    query = "SELECT * FROM staff INNER JOIN schedule ON staff.staff_id=schedule.staff_id where start_datetime <= now() and stop_datetime > now()"
    cursor = sq.ExecQuery(query)
    Do While cursor.NextRow
        data.Add(Array As String (cursor.GetInt("staff_id"),cursor.GetString("name"),cursor.GetString("password"),cursor.GetString("qualification")))
    Loop
    sq.Close
   
    ws.RunFunction("GotStaff", data)
    ws.Flush
   
End Sub

Then on the client side I recieve the info in this code
B4X:
Sub wsh_GotStaff(Params As List)
    Log("wsh_gotstaff")
    Log (Params)
    'load up a file to build the staff selector drop
    File.WriteList(File.DirInternal,"stafflist.txt",Params)
End Sub

then I use the code above to read the file and parse it so I can use it. Why did I use a file and not just pass the info via a variable? I am still learning about scope and I am struggling to sort it all out. So a txt file as the easiest way to avoid scope etc.
 
Upvote 0

IslandMedic

Member
Licensed User
Longtime User
Ok after a tonn of head scratching.. I eliminated the text file and was able to pass the list to the sub. Here is the sub I used to break apart all the data.

B4X:
Sub build_user_spinner(data As List)
    Log("build spinner staff")
    Dim record As List
    record.Initialize
  
    ' set first value as action statement  
    Spinner1.Add("Select User")
    Spinner2.Add("Select User")
  
    For i = 0 To data.Size -1
        record = data.Get(i)
        Spinner1.Add(record.Get(1) & " - " & record.Get(3))
        Spinner2.Add(record.Get(1) & " - " & record.Get(3))
    Next  
    
End Sub

not sure if this is the proper way but it seems to make sense to me. hope it helps someone else out.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…