I want to save an ArrayList so that it is persistent. The contents can change. What is the best way of approaching this in android?
2 Answers
Your question is akin to asking "I have this bag of stuff -- should I transfer the stuff to a box, bucket, barrel, or bin?".
Without knowing what the "stuff" is, we cannot easily answer the question.
Your general options are either to use a database (perhaps actually getting rid of the ArrayList
in the first place) or persistence to a file (serialization, XML, JSON, etc.).
Generally speaking, a SQLite database has advantages, in that is uses transactions and will be generally more robust than just writing your own file. However, there are things your ArrayList
might store that might not work well in a database. Of course, there are things your ArrayList
might store that might not even be able to be persisted at all (e.g., a socket, a widget).

- 986,068
- 189
- 2,389
- 2,491
-
my ArrayList contains a basic object that gets & sets strings. With that said which is the best approach? – Sheehan Alam Aug 27 '10 at 23:31
-
I guess converting it as 'GSON' and storing as file will be the easiest way – Ganapathy C Feb 22 '16 at 12:14
I am not familiar with Android SDK, but I am with Java. One thing you can easily do is serialize the ArrayList. Then when you read it again, it'll be an ArrayList object which you can readily use as if you had just created it.

- 36,800
- 16
- 93
- 123
-
2Intro to Java Serialization: http://java.sun.com/developer/technicalArticles/Programming/serialization/ – Fabian Steeg Aug 26 '10 at 23:39
-
-