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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Ok thank you very much for the answer ! is a simple way to use cvs file?
Also csv file is a simple text file with .csv extension (csv - comma seperated values)
Thank you for the code !! i prefer this way, it seems easy ^^
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

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.