0

I am really confused about how to save my ArrayList of custom objects into SharedPreference. I looked up a very popular thread but did not understand solution as certain variables were not given context. My arraylist has certain things like Strings, UUIDs, and Dates, and I do not mind going to API 11 to avoid using gson or json. If someone were to be so kind as to explain the solution in this or provide their own I would really appreciate. I am new to this, been doing for a couple of months and want to make sure I understand everything in depth but my english is not so well. Thank you in advance!

Community
  • 1
  • 1
nublit
  • 61
  • 1
  • 2
  • 5
  • [Please check this link it is help full to you][1] [1]: http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Prashant Jajal Nov 29 '14 at 09:31
  • @prshntjjl I ask about the answer to that. I do not understand what type myScores is or what scoreEditor is – nublit Nov 29 '14 at 19:55

2 Answers2

0

Serialize that arraylist and store it in sharedPreference.

Profile profile = myProfile;
String serializedData = profile .serialize();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Profile", serializedData);
editor.apply();

For Deserialize

String profileSerialized = prefs.getString("Profile", "");
profile = Profile.create(profileSerialized);
Arunkumar
  • 103
  • 1
  • 12
0

I would look into storing the data into a SQLiteDatabase. SharedPreferences wasn't intended to save custom objects. If you really want to use SharedPreferences then Gson will be the best solution. API 11 added the ability to save a list of strings, not objects.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • This is a possibility, do you know what is recommended method? Should I use db or gson or should I save as set and save as that like the thread I linked to recommends? – nublit Nov 29 '14 at 21:24
  • A db is recommended as it requires no dependencies. – Jared Rummler Nov 30 '14 at 03:17