Android Question WildCardListing problem

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I am trying to use DonManfreds WIldCardListing class to get a List of files to be shown in a listview for selection.
WCL itself seems to be working but I am failing to get the list to the Listview.
WCL correctly produces the FileListing list in the sub wcl_ListFilesFinish. To make it available to other subs I copy it to FileListing1, a list Dimmed in Sub Globals.
This copying is confirmed at the end of Sub wcl_ListFilesFinish by Log(FileListing1.Size) showing the correct size.
The same Log in the sub where FileListing1 is to be used to populate the Listview the size always shows size as zero.

B4X:
Sub Import_click
'Some code stuff

   If ExFileName_flag = 0 Then
     wcl.ClearLists   ' Clears the Dir- and Filelisting lists which will be used in Events
     wcl.ListFiles(File.DirRootExternal & "/Download/",True, "*.txt",True, True)
     Log("Filelisting1 size = " & FileListing1.Size)   'Always Logs "Filelisting1 size = 0"
     'Code stuff
   End If

   'More code stuff
End Sub


Sub wcl_ListFilesFinish(FileListing As List)        'Event called by wcl.ListFiles
    Log("wcl_ListFilesFinish("&FileListing.Size&")")
    Dim TempF As String
    For i = 0 To FileListing.Size -1
       ' Log(FileListing.Get(i))
    Next

    FileListing1.Clear
    For i = 0 To FileListing.Size -1      'Copy FileListing to FileListing1
        TempF = FileListing.Get(i)
        FileListing1.Add(TempF)
    Next
    Log(FileListing1.Size)            'Always shows correct size. Copy works
End Sub


Thanks in advance

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Roycfer,

The ListView code is in Sub ImportFileSelect. It is called from Import_click in the bit shown above as "More code stuff".
I don't think the ListView code is relevant as I appear to lose the data in FileListing1 before this sub is called but code is below.

B4X:
Sub ImportFileSelect
'Brings up ListView to allow selection of files.
'Called by Import_click
    Dim temp As String
    ListFlag = 1
    ExFileName_flag = 1
    ImportFileView.Initialize("ImportFileView")
    Activity.AddView(ImportFileView, 0, 10%y, 100%x, 90%y) 
   
'Set the parameters for displaying the Items of the ListView
    FileLabel = ImportFileView.SingleLineLayout.Label    
    FileLabel.TextColor = Colors.White   
    FileLabel.TextSize = 14*FontScale
    FileLabel.Color = Colors.DarkGray                
    FileLabel.Height = 5%y                               'Label height and Item Height should match to avoid gsps & overlaps
    ImportFileView.SingleLineLayout.ItemHeight = ImportFileView.SingleLineLayout.Label.Height
    ImportFileView.Color = Colors.DarkGray          'ListView color matches Label color
    ImportFileView.Clear                                   'Clear Listview before populating
   
    For FileN = 0 To FileListing1.Size-1                'Builds the viewable list ImportFileView from the List "FileListing"
        temp = FileListing1.Get(FileN)                   'Get the value from the List
        ImportFileView.AddSingleLine(FileN)           'Store the value in the ListView
    Next

    'Title label and a "Back" button
    lblTitle.Text = "Select File"
    lblTitle.BringToFront
    lblTitle.Height = 10%y
    lblTitle.width = 100%x
    lblTitle.Top = 0
    ListBack.BringToFront
    ListBack.Height = 10%y
    ListBack.Top = 0
End Sub


Regards Roger
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
At some point, you have to call ImportFileView.Invalidate to force it to redraw itself. I'm not seeing that call in your code.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Roycefer,


I appreciate the feedback but I think we are off topic. I don't have a problem with ListView.
Be assured I will address "ImportFileView.Invalidate" when I get to "Sub ImportFileView_ItemClick (Position As Int, Value As Object)"

In the Sub Import_click the line: wcl.ListFiles(File.DirRootExternal & "/Download/",True, "*.txt",True, True) Adds items to Filelisting1 via Sub wcl_ListFilesFinish .
The next line of code: Log("Filelisting1 size = " & FileListing1.Size) shows no items in Filelisting1, this is the issue.


Regards Roger
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
The line
B4X:
Log("Filelisting1 size = " & FileListing1.Size)
is bound to show 0 because it is executed immediately after wcl.ListFiles whereas wcl.ListFiles causes wcl to fire the wcl_ListFilesFinish event. But that event is added to the end of the event queue. It won't be executed until after the current execution completes (namely the function stack in which Import_click resides). And you can't be guaranteed of the results you want (filling FileListing1) until that event executes. In short, you need to fill the ListView in the wcl_ListFilesFinish event sub (or after you know it has executed) because that's the earliest time when you can be guaranteed that FileListing1 has been filled.

If you find this event system awkward to use, you can try LucaMS's suggestion and use a synchronous class to accomplish this task. But beware of ANRs.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi LucMs,

First, no I haven't filled the ListView with anything, problems is there right from fresh install.
Second, Log file only shows the Logs I requested. List size Etc. no errors or alerts.
Thirdly I will have a detailed look at the "Wonderful" alternative.

Does "GetFilteredFileList" return a list of files only, no directories? I am only trying to list text files in the download folder.

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
The line
B4X:
Log("Filelisting1 size = " & FileListing1.Size)
is bound to show 0 because it is executed immediately after wcl.ListFiles whereas wcl.ListFiles causes wcl to fire the wcl_ListFilesFinish event. But that event is added to the end of the event queue. It won't be executed until after the current execution completes (namely the function stack in which Import_click resides). And you can't be guaranteed of the results you want (filling FileListing1) until that event executes. In short, you need to fill the ListView in the wcl_ListFilesFinish event sub (or after you know it has executed) because that's the earliest time when you can be guaranteed that FileListing1 has been filled.

If you find this event system awkward to use, you can try LucaMS's suggestion and use a synchronous class to accomplish this task. But beware of ANRs.


Thanks Roycefer, this is enlightenment. Now that I know whats happening I'll work around it.

Regards Roger
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hi LucMs,

First, no I haven't filled the ListView with anything, problems is there right from fresh install.
Second, Log file only shows the Logs I requested. List size Etc. no errors or alerts.
Thirdly I will have a detailed look at the "Wonderful" alternative.

Does "GetFilteredFileList" return a list of files only, no directories? I am only trying to list text files in the download folder.

Regards Roger


I asked you to look at the log, so that you can know if the event is executed before loading the ListView.

I do not remember "my class" :D, but I think that class served as an alternative to the native command, which also returns directories, mine files only.
 
Upvote 0
Top