1

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?

TimeParadox
  • 139
  • 1
  • 1
  • 14

3 Answers3

2

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

Community
  • 1
  • 1
Optimus
  • 2,716
  • 4
  • 29
  • 49
1

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

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • yes ! i've read this docs but i don't understand what i've to do in my specific situation, i think i have to use internal storage ( my data will be not bigger than 400/500Kbyte.. – TimeParadox Nov 10 '11 at 11:02
0

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.

Community
  • 1
  • 1
Moss
  • 6,002
  • 1
  • 35
  • 40