Reputation: 139
I've read many topic on stackoverflow,and google, about this argument but i can't orient myself with writing custom ArrayList into files ! I've an ArrayList of type "CustomType". "CustomType" have two String variable : "name" and "description".
I want to save this ArrayList for reading it after closing and reopening my app. Does anyone help me doing this? and does anyone explain me what happen when i save/read?
Upvotes: 1
Views: 901
Reputation: 2786
you could write it simply as a csv file, like this,
1,name,desc
2,name2,desc2
3,name3,desc3
...
or you could use gson
library - http://code.google.com/p/google-gson/, to convert it into a json
string and then write it to a file, that way when you read it you could directly get the object from json
,
i would personally use the second method,
Edit: for writing to csv
try
{
FileWriter writer = new FileWriter("<path>/MyFile.csv");
while(lst.next())
{
for(int i = 0; i < columnSize; i++)
{
writer.append(i);
writer.append(',');
writer.append(lst.get(i).getName());
writer.append(',');
writer.append(lst.get(i).getDesc());
}
writer.append('\n');
}
}
catch(Exception e)
{
e.printStackTrace();
}
(assuming the object in side the ArrayList has methods getName()
and getDesc()
)
this might be helpful in reading it again,
Get and Parse CSV file in android
Upvotes: 2
Reputation: 6012
You could use XML files too:
This way you can use all kind of text and symbols, if you are using a CSV you can not use the 'separator' in your content.
Upvotes: 0
Reputation: 8242
To save any kind of data you have to choose any of the available data storage tequinue
1)shared preferences
2)internal storage
3)external storage
4)Sqlite
5)internet server
detailed docs here
throgh any one of these way you can store ArrayList or other data which you are using to populate the screen , so every time user will open the app data will be loaded from stored location
Upvotes: 1