Android Question Skip emtpy line by reading in a file to a listview

GMan

Well-Known Member
Licensed User
Longtime User
i am using the following code to read in a file and show it on a listview.

TextReader:
    Dim TextRd As TextReader
    TextRd.Initialize(File.OpenInput(File.DirAssets, "Bestandsliste.txt"))
    ListViewBestand.Clear
    ListViewBestand.SingleLineLayout.Label.TextSize=14
    ListViewBestand.SingleLineLayout.Label.Height=20dip
    ListViewBestand.SingleLineLayout.ItemHeight=70
    ListViewBestand.SingleLineLayout.Label.TextColor = xui.Color_Black

    Dim line As String
    line = TextRd.ReadLine
'    ListViewBestand.s = 120dip
    ListViewBestand.AddSingleLine(line.trim)
    
    Do While line <> Null
        line = TextRd.ReadLine
        ListViewBestand.AddSingleLine(line)
        Log(line)
    Loop
    TextRd .Close
    ListViewBestand.Invalidate

As some lines are empty i want to skip them when reading in OR displaying the items in the listview.
Anyone a hint ?
 

JohnC

Expert
Licensed User
Longtime User
ChatGPT says...

To skip empty lines when reading from the file and displaying them in the ListView, you can simply check whether the line is empty or contains only whitespace. Here's how you can modify your code to achieve this:
B4X:
Dim TextRd As TextReader
TextRd.Initialize(File.OpenInput(File.DirAssets, "Bestandsliste.txt"))
ListViewBestand.Clear
ListViewBestand.SingleLineLayout.Label.TextSize = 14
ListViewBestand.SingleLineLayout.Label.Height = 20dip
ListViewBestand.SingleLineLayout.ItemHeight = 70
ListViewBestand.SingleLineLayout.Label.TextColor = xui.Color_Black

Dim line As String
line = TextRd.ReadLine

Do While line <> Null
    If line.Trim.Length > 0 Then ' Skip empty lines
        ListViewBestand.AddSingleLine(line.Trim)
        Log(line)
    End If
    line = TextRd.ReadLine
Loop

TextRd.Close
ListViewBestand.Invalidate
The key addition is If line.Trim.Length > 0 Then, which checks whether the line is not empty after trimming any leading or trailing whitespace. If the line is not empty, it gets added to the ListView.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
This is writing the 1st line of the list endless in the Log
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I ran the new issue through the new ChatGPT o1 (preview version) and this is what it said...

The issue arises because reading line by line from a file located in File.DirAssets using TextReader does not work as expected in B4A. The assets folder is read-only and has limitations when accessed directly with TextReader. Instead, you should use File.ReadList to read all lines at once into a List.

Here's the corrected code:

B4X:
Dim lines As List
lines = File.ReadList(File.DirAssets, "Bestandsliste.txt")

ListViewBestand.Clear
ListViewBestand.SingleLineLayout.Label.TextSize = 14
ListViewBestand.SingleLineLayout.Label.Height = 20dip
ListViewBestand.SingleLineLayout.ItemHeight = 70
ListViewBestand.SingleLineLayout.Label.TextColor = xui.Color_Black

For Each line As String In lines
    If line.Trim.Length > 0 Then ' Skip empty lines
        ListViewBestand.AddSingleLine(line.Trim)
        Log(line)
    End If
Next

ListViewBestand.Invalidate

Explanation:
  • Use File.ReadList: This function reads all the lines from the file into a List, which works correctly with files in the assets folder.
  • Replace TextReader with a List: By reading all lines into a List, you can iterate over each line without issues.
  • Keep the ListView Configuration: Your ListView settings remain the same.
  • Iterate Over the List: Use a For Each loop to process each line.
This modification ensures that all lines from "Bestandsliste.txt" are read correctly and added to the ListView without the endless repetition of the first line.
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
This does the trick !

there are also some line starting with a Asterisk(*) - these i want to exclude
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…