Populate ArrayList from CSV

linum

Active Member
Licensed User
Is it possible to populate an ArrayList from data stored in a csv file? Could somebody please post an example?


Thank you...
 

Mr_Gee

Active Member
Licensed User
Longtime User
B4X:
'load csv
table1.loadCSV("filename.csv",",",true,true)

'read line by line
for i=0 to table1.count -1
'and add to arraylist, don't forget to fill in columnlist
   al1.add(Table1.Cell(Columname,i))
Next
or the below, depending on the data
B4X:
please note this will not pick up the first item, but this usually has the column names
fileopen(c1,filename,cRead)
s = Fileread(c1)
Do Untill s = EOF
s = Fileread(c1)
     'optional : If NOT(s = "@eof@") Then 
   al1.Add(s)
Loop
Fileclose(c1)
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
the straight answer is yes...
But it all deppends on how you are readding the csv..
are you using the table control to read/load the data to the table or using FileRead?

PS. Mr_Gee beatted me to the clock...
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
B4X:
'load csv
table1.loadCSV("filename.csv",",",true,true)

'read line by line
for i=0 to table1.count -1
'and add to arraylist, don't forget to fill in columnlist
   al1.add(Table1.Cell(Columname,i))
Next
or the below, depending on the data
B4X:
please note this will not pick up the first item, but this usually has the column names
fileopen(c1,filename,cRead)
s = Fileread(c1)
Do Untill s = EOF
     'optional : If NOT(s = "@eof@") Then 
   al1.Add(s)
Loop
Fileclose(c1)

Mr_Gee, you need an extra "s=FileRead(c1)" before the "Loop" or the "s" value is never changed...
 

linum

Active Member
Licensed User
Wow! I just got a chance to log in and I find nothing but great answers, thank you so much.

I still don't know how I want to load my data. Is it easier to load it into a table and then into an arraylist?

I'll try the example and go from there...
 

Mr_Gee

Active Member
Licensed User
Longtime User
It all depends on how you have your data structured and what you want to do with it...

I think the 1st option is the nicest,
since you are populating only the required data..
 
Top