1

I have saved ArrayList of custom objects in the shared preferences like this:

SharedPreferences prefs = context.getSharedPreferences("prefName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putStringSet("myList", new Gson().toJson(arraylist).toString());
editor.apply();  

By doing this, once values are saved but when i exit from the app and relaunch and try to save new values then the old values are gone.
Any idea how can i save old values and append new ones?
I want to keep all values in the same array and keep array saved to show values every time when the app loads.

2
  • 1
    Naturally the old values will be gone. If you need to append, then you need to read the original ones, append the new ones and save back. Commented Mar 6, 2013 at 8:41
  • @AleksG.. okay man i implement this.. Thanks Commented Mar 6, 2013 at 8:49

3 Answers 3

1

Read myList from prefName, append arraylist to what was already saved in the preferences, write back myList to preferences.

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

Comments

0

This code may be work for you.

 SharedPreferences setting=context.getSharedPreferences("prefName", Activity.MODE_PRIVATE);
 SharedPreferences.Editor edit=setting.edit();
 edit.putStringSet("myList", new Gson().toJson(arraylist).toString());
 edit.commit();

4 Comments

whenever you want to change the value put this code in method. call that method once again.
I would really like to know who upvoted this answer because it does nothing but replace apply() by commit() which is definitely not the cause of the problem here. Quite to the contrary, it could even create a performance hit on the UI thread.
I am already doing it man.. Its just saving the things into prefs... Please read my question again.
Get that listArray and append the new value in the list then after store it in shared preference.
0

I hav faced this problem earlier... Use generics it will append the value rather than replacing the old datas....

1 Comment

@Noman Kindly read how to use generics in java.. It may be useful for your future problems...

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.