I have an arraylist. I need to store them as persistent data. How can I achieve this in android? Can shared preferences help?
-
1You can use SharedPreferences but there you'll have to convert your data in some manner, since there is no `putArrayList()` method. – Sam Jan 25 '13 at 20:59
-
1Put into shared preferences as already explained and discussed in [this question](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) – Audrius Meškauskas Jan 25 '13 at 21:01
3 Answers
There are several ways depending on what the data is and what you plan to do with it. SharedPrefs, SQLite DB, and internal storage are all options. But like I said, it depends on what you are going to do. The Docs explain all of these and you will need to decide what fits you best.
- 44,229
- 13
- 74
- 91
I agree with codeMagic. It depends on what the data is and what you are going to do with it.
Following is a brief explanation of which storage option to use at what time :
Shared Preferences: These are basically good for storing an applications preferences and other small bits of data.SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. So it becomes easy to read the data. But for large data sets it might become a bit difficult because you have to define a unique key for every key in the dataset. Ex: Store session key for user login in SharedPreferences.
Internal Storage: This is good for storing application data which the user doesnot need access to. Possibly good for storing logs or anything that the application will create/update/delete.
SQLite: Large amounts of same structured data should be stored in a SQLite database as databases are designed for this kind of data. As the data is structured and managed by the database, it can be queried to get a sub set of the data which matches certain criteria using a query language like SQL. Searching becomes easy here. While SQLite might not scale nearly as big as the dedicated databases, it is very quick and convenient for smaller applications, like Android apps. Ex: Storing your listview items OR storing recipes for a recipe app.
Hope this helps!!!
- 3,758
- 1
- 24
- 40
I don't think you will be able to use SharedPreferences for an ArrayList in a simple way (even in anyway) as there is not some putObject() or putSerializable() method in the SharedPreference.Editor class.
But there are several others storage options.
- 8,434
- 4
- 48
- 67