Android Question Possible Bug with Listview

ElliotHC

Active Member
Licensed User
For some really strange reason when I run this code the ListView doesn't get filled... The Log output show the entries no problem but the listview is blank.

If I remove this

B4X:
        If ListView_Runs.size > 0 Then
            For i = 0 To ListView_Runs.size - 1
                If ListView_Runs.GetItem(i) = Parser2(0) & "s ," & Parser2(1) Then
                    Entry_Exists = True '<<<<<<<<
                End If
            Next
        End If

Then as you'd expect I get an endlessly filled looping list.

But with it in place the code does exactly what I expect it to but the listview does not get filled.

B4X:
Sub Read_Text ' And compile the list based
    
    
    Try
        
    Dim Entry_Exists As Boolean
    
     Dim Text As String = File.ReadString(FilePath, DateTime.Date(DateTime.Now)  & ".txt")
    

    If Text.Length > 15 Then

        Dim Parser() As String
        Dim Parser2() As String
                
        Parser = Regex.Split("/", Text)

    For i = 1 To Parser.Length             
        
        Parser2 = Regex.Split(",", Parser(i))

        Entry_Exists = False

        If ListView_Runs.size > 0 Then
            For i = 0 To ListView_Runs.size - 1
                If ListView_Runs.GetItem(i) = Parser2(0) & "s ," & Parser2(1) Then
                    Entry_Exists = True 
                End If
            Next
        End If

        If Parser2(0) <> "9.999" Then
            If Entry_Exists = False Then
                ListView_Runs.AddSingleLine(Parser2(0) & "s ," & Parser2(1)) ' But No listview entries??
                Log(Parser2(0) & "s ," & Parser2(1)) ' <<<<<<<<< See this below
            End If
        End If

    Next   

    End If
    Catch ex As Exception
        Log(ex)

    End Try
End Sub

upload_2019-10-15_13-17-2.png
 

stevel05

Expert
Licensed User
Longtime User
Are you sure that the filename you are opening is correct? It looks like it's trying to open a file that was just created.

B4X:
File.ReadString(FilePath, DateTime.Date(DateTime.Now)  & ".txt")
 
Upvote 0

ElliotHC

Active Member
Licensed User
Are you sure that the filename you are opening is correct? It looks like it's trying to open a file that was just created.

B4X:
File.ReadString(FilePath, DateTime.Date(DateTime.Now)  & ".txt")

My DateTime.Now is purely looking at Years, Months and Days so there is a new file for each day.

B4X:
DateTime.DateFormat = "yyyyMMdd"
 
Upvote 0

ElliotHC

Active Member
Licensed User
There is no other cause for this that I can see.. The entry is there clear as day in the Log output.. Why is it not getting added to the list?

If I remove the flag then I get a massive looping list as you would expect.

I only see a bug here but I'd love for someone to find another reason... It's holding up my project :(
 
Upvote 0

emexes

Expert
Licensed User
Check array indexing in:
B4X:
Parser = Regex.Split("/", Text)

For i = 1 To Parser.Length           
    Parser2 = Regex.Split(",", Parser(i))
Array indexing in B4X is usually 0 to N - 1, rather than 1 to N
 
Last edited:
Upvote 0

ElliotHC

Active Member
Licensed User
Array elements are numbered 0 to N - 1

ie not 1 to N

That is correct. Although I am not interested in the data at array position 0 and adding the - 1 doesn't make the items appear on my listview.

This is really strange I have to say...
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As a test, try adding a sleep(0) statement directly after the log to allow the gui to update after each added entry to see if they are being added.
 
Upvote 0

emexes

Expert
Licensed User
Relying on Try..Catch to enforce array index bounds feels a bit like pointing a loaded gun towards one's foot, but... if you're ok with it, then go for it.

Having got that off my conscience:

you're right about the log yes, list no situation being odd. Three ideas:

1/ after you add each item to the list, Log(ListView_Runs.Items.Length) or whatever gets number of items in ListView
2/ do a Sleep(1) after adding each item to ListView_Runs
3/ add a temporary test item after the ListView_Runs is initialized or loaded via layout, see if that shows up on screen

Edit: stevel05 beat me by 10 seconds :) great minds think alike, but my mind is slower
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Damn, you are using the variable i as an index for both loops, this will fail.
Can't believe I missed that; did it myself just last week.

But... doesn't explain why item shows in Log but not in ListView. That's got me intrigued. Sleep might fix, but... most programs manage to update the display without explicitly relinquishing control to the message handler loop, so: why is this case different?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It depends what is happening after the sub completes, if it goes on to do something else intensive it may not have a chance to update. It is always difficult to visually debug code in isolation.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
In fact, the way the indexes are, the variable i will be reset by the second loop and an infinite loop will be created, not allowing the gui to update.

Edited reponse.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
This is what I sometimes use for hunting down elusive bugs:

KangarooGun960.jpg


Image shrunk down to to fit within forum size limit. The text reads:
KANGAROO RIFLE: The computer-designed barrel of this weapon gives the shell an undulating trajectory, which follows the leaping movement of the animal. (Spectacular results obtained! Many testimonials from our clients the world over!)
 
Upvote 0
Top