Android Question Problem Adding Data to Spinner View

GGS

Member
Licensed User
Longtime User
Hi Everyone,

I am working on an app that needs to copy a text file from a network server to the device, and then display the file in spinner view or a list view. This is the code that I think should work, but does not:

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("TestLayout") 'load the screen layout
If FirstTime Then
SMB1.Initialize("SMB1") 'load the SMB File library
End If

'Load the data file path to the network server
If File.Exists(File.DirInternal,"URLPath.txt") Then
URLServer = File.ReadString(File.DirInternal,"URLPath.txt")
Else
Msgbox("Setups are not complete.","Complete Setups")
End If

'load the department file from network server
SMB1.DownloadFile("smb:" & URLServer ,"tabdep.txt", File.DirInternal,"tabdep.txt") 'load department file

'problem code below?

Dim reader As TextReader
Dim Dline As String
reader.Initialize(File.OpenInput(File.DirInternal,"tabdep.txt"))
Dline = reader.ReadLine
DepSpinner.AddAll(Regex.Split(",",Dline))
End Sub

The section labeled "problem code below?" works fine if it is placed under a button somewhere on the layout that the user taps. The spinner control displays the items correctly. When the code is inserted as above, the application appears briefly on the tablet device, then disappears. I'm connected to an Android tablet using the Bridge wireless option (which seems to work very well).

Any ideas where I'm going wrong? I'm a very experienced VB6 programmer, but this has me a little lost.


 

stevel05

Expert
Licensed User
Longtime User
Welcome to the Forums

Run the app in Debug mode and you should see an error in the log when it fails.

Also please use the [ Code] and [ /Code] tags (without spaces) when posting code
 
Upvote 0

GGS

Member
Licensed User
Longtime User
Hi,

Changed the code to this:
B4X:
Sub Activity_Create(FirstTime As Boolean)    'Do not forget to load the layout file created with the visual designer. For example:
  
    Activity.LoadLayout("TestLayout")                'load the screen layout
    If FirstTime Then
        SMB1.Initialize("SMB1")                        'load the SMB File library
    
    End If
  
    If File.Exists(File.DirInternal,"URLPath.txt") Then
        URLServer = File.ReadString(File.DirInternal,"URLPath.txt")
    Else
        Msgbox("Please click on the tool icon and complete the setup information.","Complete Setups")
    End If
  
    'load the department file from network server
    SMB1.DownloadFile("smb:" & URLServer ,"tabdep.txt", File.DirRootExternal,"tabdep.txt")    'load department file
  
    Dim Reader As TextReader
    Dim line As String
    Reader.Initialize(File.OpenInput(File.DirRootExternal,"tabdep.txt"))
    If Reader.IsInitialized Then
        line = Reader.ReadLine
        Do While line <> Null
            DepSpinner.Add (line)
            line=Reader.ReadLine
        Loop
        Reader.Close
    End If
  
  
End Sub

This code works fine and the spinner is populated with data from the "tabdep.txt" file if I step through the code in debug mode. The Spinner is empty if I just run the code without stepping through it. It seems the problem is the code to populate the Spinner control is executing before the SMB1.Download command is complete. It works fine too if I put the code in the DownLoadCompleted sub, but I have several setup type files to download from the server before the app is available to the user for input. In addition, the app will download files from the server after various types of user input. It can't be this tricky just to download a file and display the contents. Am I missing something simple? There are no errors in the log file that I can see.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You've found your problem, you just need wait for the download to complete before processing the data.

I've not used the SMB library, I don't know if you can download more than one file at a time. It looks like you can check the URL in the DownloadCompleted sub to see which job has finished. Or, download them in turn.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…