I am using the tableview class and populating it from a csv file.
I want to get rid of some columns (not just hide them) and save as a new csv file.
I cannot see how to remove columns. Can anyone help?
You cannot remove columns in the Table Class.
You need to do it on your own.
You could read your csv file with StringUtils.LoadCSV in a List of String Arrays, each array representing a row.
And create a new List of String Arrays with only the columns you want and save this List with StringUtils.SaveCSV.
I have loaded the csv file (which has 28 columns - no headers and blanks in the first row)
Dim List1 as List
List1=StringUtils.Loadcsv(Files.DirDefaultExternal,"DataFile.csv",",")
But how do I read the List1, say get the value in row2, column6 and insert it into row8, column3?
What ever I try I get a Ljava.lang.string errors.
Thanks
Kintara
Posting note for myself here.
OK, using this code I can see each element of the List1
Dim List1 as List
List1=StringUtils.Loadcsv(Files.DirDefaultExternal,"DataFile.csv",",") 'List1 loaded from a CSV file, 28 columns and 800 rows of data
'(List1 is an array 28 wide by 800 long)
'(this will read one row from List1 at a time then show one column at a time from that row)
For i = 0 To List1.Size - 1
Dim cells() As String
cells = List1.get(i) 'cells will be an array 28 wide and 1 long. It is loaded with one row from List1
for c = 0 To cells.Length - 1 'Now to look at the row in detail, one column at a time
Log(cells(c)) 'this is the contents of Row(i), Column(c)
Next 'Next Column
Next 'Next Row
Using this method I should be able to read Row(x), Col(y)
r=2 'Row number 3
c=8 'Column number 9
Cells = List1.get(r) 'read the row into Cells
ValueOfCell=Cells(c) 'read the column from position c in Cells
Klaus, you are correct "In your code table.get should be List1.Get."
I was typing in the code and made a few typo errors. (I cannot cut and paste as I have to use different PC's for running B4A and another for accessing the forum due to IT restrictions at work)
To set the value of the list at Row x, Column y I have used:
B4X:
'read the row into an array, each element of the array represents a column.
Cells = List1.get(0) 'read the top row into Cells' This row contains the headers, but they are not the names I wish to use so I want to rename them
'Setting a value to the cells(y) seems to directly set the value in List1 row r, column c.
'These are to be used as column headers when eventually loaded into a SQL table.
Cells(0)="Item" 'column 0
Cells(1)="Description" 'column 1
Cells(2)="Bin" 'column 2
Now I want to add a new row just below the headers; i.e. a new row at row 1.
I am using :
B4X:
List1.InsertAt(1,Cells) 'to insert a duplicate of the first row
'then reading that row into Cells
Cells = List1.get(1)
'then setting the values of Cells() hoping it would change the values in the row just added to List1:
Cells(0)="1" 'column 0
Cells(1)=IssueDate 'column 1 "IssueDate" is a string variable
Cells(2)=IssueTime 'column 2 "IssueTime" is a string variable
This is not correct as it seems to also change the values in the top row (List1(0)) as well!! Hmmmm. Obviously my understanding of Lists and arrays is a little lacking. More reading follows!
OK. The solution is to use the following to add a new row with values as indicated:
B4X:
List1.InsertAt(1,Array As String ("1", "Vocab","Void",IssueDate, IssueTime)) 'to insert a new row with the given values.
'Perfect!
'Now to save the List1 to a csv file:
StringUtilities.SaveCSV(Files.DirDefaultExternal,"NewFile.csv",",",List1)
HI, Klaus. The code snippets are really aid memoirs for me as I'm not too familiar with how lists and arrays work and this is my kinda note pad.
From what I have read you are suggesting something like
List2.Addall(List1) (not fully up on the correct syntax but I do know i cannot use List2=List1 for reasons expressed in other posts.
The last code snippet seems to do what I want it to do: do i really need to set up another list before saving to a csv?
Hi Klaus.
I'm really using this as a notepad to remind myself in future of my endeavours.
The last code snippet seems to do what I want so do I need to set up a new List?
Are you suggesting i should do something like List2.AddAll(List1)? I know I cannot use List2=List1 as this posting shows. https://www.b4x.com/android/forum/threads/making-list2-list1.30831/#post-197857
All I want to do is load a csv file, amend the contents of the top row so they will act as headers, add a new row just below the headers with file ID and date, delete some rows, put it into a SQl table and finally reduce the number of columns.
I have figured out each step, but some steps seem to take a long time (especially saving as a CSV the entering it into a SQL table)
The original csv file has 28 columns by 9500 row. The final will have 5 columns and 5 less rows.
This is no more the same problem as in post#1.
Some questions to make sure I understand correctly:
1) You have a csv file without headers ?
2) You want to add a line for the headers ?
3) You want to add another line below the headers with different data than the columns ?
4) You will remove 23 columns and 5 rows ?
5) Save the new data to a new csv file ?
6) Fill a SQL table, SQL table or the Table Class ?
HI Klaus,
Thanks for your post.
You are correct on all points. To clarify point 6 will be to fill a SQL table within a SQL database.
The 1st post was the start of the full task, and was the initial problem but as I have gone on I realised I did not understand lists the way I thought so the thread has expanded a bit.
I'm getting there and don't expect a anyone to post fully formed answer without me doing some learning. This post is my learning log.
I would do it that way:
Create the database.
Read the csv file.
Go through the List.
Read each line.
Remove the columns in the line.
Add the modified line to the SQL table.
Good Plan.
I'll look into it but it seems a good way to go .
I'm not sure how to remove the columns in the line, but that's what learning is all about.
The only shortcoming that I can see is that the headers will need to be set on Table creation.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.