0

I am using sharedPreferences for storing certain information from the user. I am not using any sort of database at all, and I also dont want to use any unless necessary. My activity displays a recyclerView with 10-13 items (Dynamically added by user).

Currently I am using an arrayList to inflate data in the recyclerView. According to rules, It isn't possible to save an arrayList in sharedPreferenes (although there is a hack to do so, but its not recommended). So, instead, I want to save data in an array (two dimensional) and save it in sharedPreferences and inflate the recyclerView from the data in that array.

Is it even possible? Is it the right way for achieving what I want? I am new to this, please guide.

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52
  • 1
    These might be helpful. [1](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) [2](http://stackoverflow.com/questions/3876680/is-it-possible-to-add-an-array-or-object-to-sharedpreferences-on-android). – Uma Kanth Sep 09 '15 at 11:08

1 Answers1

1

Is it even possible?

More or less, you can convert an array to an arraylist and then pass it to the recyclerview, but

Is it the right way for achieving what I want?

Not really, just use the datatype that fits the situation. If you need an arraylist for the recyclerview adapter, just use that.

there is a hack to do so, but its not recommended

I don't know what hack you speak of, or where you heard that it's not recommended, but serializing an ArrayList, even with custom objects, is very possible and this technique is widely used by android developers.

You should have a look at Serializable and Parcelable interfaces, they allow you to store an arraylist in sharedpreferences.

Tim
  • 41,901
  • 18
  • 127
  • 145