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
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
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.
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
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.
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.
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
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
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.
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.
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
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.
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