B4J Question Import ascii file into a tableview

GuenterL

Member
Licensed User
Longtime User
Hi all,
I do have a short question:
I want to fill up a tableview with values stored in a comma separated ascii file. The file valus look like:
...
Value,Value,Value
...
Values can be empty.
Now it try to read the file and import the data into a tableview using the following code:

B4X:
Dim line As String
Dim str() As String
If fileName <> "" Then
    Reader.Initialize(File.OpenInput("", fileName))
    Do While line <> Null
    line = Reader.ReadLine
    str = Regex.Split(",", line)
    Log(str(0))
    TableView1.Items.Add(str)
Loop
   Reader.Close
End If

The log display correctly the first value, but it seems, that str(1) and str(2) are empty.
What do i wrong ?? I am struggeling with this since 2 days !!
Thanks for your help in advance
GuenterL
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

try like

B4X:
Sub FillTableView
   Dim line As String
   Dim l As List = File.ReadList(File.DirApp, "test.csv")
   For Each line As String In l
     TableView1.Items.Add(Regex.Split(",", line))
   Next
End Sub
 
Upvote 0

GuenterL

Member
Licensed User
Longtime User
Thanks for the swift answer.
Very interesting, i used your code,but NOTHING happens !!
May be the problem is something totally different. Do I have to update the tableview, because
the tableview is displayed empty, but trying to enter another value manually, it ask to
overright existing (not visible) values.

Another idea ??
Guenter
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Another idea ??
Create a small project which shows the problem. Export it as zip and upload it here.
Seeing (and running) the code is most probably easier to find the problem than from code snippets
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Attached the testproject I used:
Defined the tableview with the B4J Internal Designer, then run filltableview when starting the project. No more changes to the tableview made.

Hope this helps.
 

Attachments

  • TableViewImportCSV.zip
    2.4 KB · Views: 311
Upvote 0

GuenterL

Member
Licensed User
Longtime User
Hi Rob,
i tried your testproject... and it works fine.
I tried it also with my file, and it fails

The file is called test.txt and looks like this:
00:06:18,,
00:03:56,1.4,21.3
00:02:05,0.8,22.9
00:11:18,,
00:04:17,1.6,22.4

strange......
Guenter
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Did you create the tableview in code or via the designer?
If you created it via code the columns get squished unless you give them a width, so it looks like the table is empty.
Try dragging the column headers over (the mouse changes to a <-> on the column boundaries) to see if you can see your data in the table.

csvtable.png

Using your data.
B4X:
	For Each line As String In Regex.Split(Chr(10),File.ReadString("../","tst.csv"))
		tv.Items.Add(Regex.Split(",",line))
	Next
 
Last edited:
Upvote 0

GuenterL

Member
Licensed User
Longtime User
Hi,
the tableview was created in designer.
But there is a strange other issue:
The example from Rob aslo works on my machine.
But using my file, if fails
Java Error says: "The system cannot find the file specified"
???????
Using same dir, same user (administrator) file permission is the same
csv works fine, txt fails

guenter
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Silly question, do you have the file open in another app ? it may have an exclusive lock on it.
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
The issue is with the csv file. all cells must be filled, else regex.split returns not all cells as expected by the tableview (idc 3 cells).
If the csv file contains at least a blank for each cell then it works (like below).
This means, that if the csv file has different number of filled cells per row, the code has to cope with that = per line checks are required to then set the tablerow cells accordingly.

B4X:
00:06:18, ,
00:03:56,1.4,21.3,
00:02:05,0.8,22.9,
00:11:18, ,
00:04:17,1.6,22.4,
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Odd, it works the same for me whether the file is called .csv or .txt and without spaces between the commas for blank items, and without the trailing comma on each line.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Deastrum: your code at post #7 works fine, means using file.readstring instead of file.readlist. Good learning. Thanks for that.

Testproject in Post #5 updated.
 
Upvote 0

GuenterL

Member
Licensed User
Longtime User
Good New, bad news
First, it works fine now, it loads the values and fills the table.
But after that, it locks the whole application, so no additional action is possible.

Do I have to close the file (like in B4a ?)
Attached now, the code of the whole module.

Thanks in advance
Guenter

PS.: where is the logfile stored. I am not able to copy/paste it from the editor.

B4X:
Sub mnuLoad_Action
   
    Dim ret As String
    Dim fc As FileChooser
   
    fc.Initialize
    fc.InitialDirectory = File.DirApp
    fc.Title = "Load Time list"   
    fc.SetExtensionFilter("Sequence", Array As String("*.seq"))
   
    If TableView1.Items.Size > 0 Then
        ret = msgBox.Show2("overwrite existing list?","", "Yes", "No", "")
        If ret = DialogResponse.POSITIVE Then
            TableView1.Items.Clear
        Else
            Return
        End If   
    End If
   
    Dim fileName As String = fc.Showopen(MainForm)
    Dim line As String
    Dim str() As String
   
    If fileName <> "" Then
       
        For Each line As String In Regex.Split(Chr(10),File.ReadString("",fileName))
        TableView1.Items.Add(Regex.Split(",",line))
           Next
       
    End If   
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
As far as I am aware the readstring simply reads then closes the file.

Try removing the
B4X:
Dim line As String
line as the for each line defines the line variable too. It shouldn't make a difference though.

As Rob said earlier
There is something weird going on with the split.

The split is introducing an extra CHR(13) so you get eg from 1,, split on ,
(array 0) 1
(array 1) a zero length item as expected
(array 2) chr(13) - not the zero length item I would expect.

It's not reading the chr(13) from the file as I replaced all CRLF with chr(10) in the string from the file.

Most odd......
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
As an aside - would it not be logical to have the other file reading routines like loadcsv etc in the File.??? routine.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

GuenterL

Member
Licensed User
Longtime User
Erel, your right !! (again). Why sould I redesign the wheel.
I replaced my load/save stuff with Load/SaveCSV and everything was fine.
Thnaks for your help
Guenter
 
Upvote 0
Top