Append more values to Shared Preferences rather than overwriting the existing values
android, sharedpreferences
Solution
Follow these steps:
Extract the value stored in `SharedPreferences`
String value = prefs.getString(<Key>, <DefaultValue>);
Append to the extracted value
String appendedValue = append(value, newValue);
Write the result back to `SharedPreferences`
editor.putString(<Key>, appendedValue).commit();
Problem
In my app, I need the values to be saved in to Sharedpreferences file RKs_Data without overwriting the existing data. Every time, I click 'Yes' in my app, I require all the values stored in the 'RKs_Data' instead of just having the latest 'name' and 'phoneNo' in to the file. - Is it possible to do so through SharedPreferences ? If yes, how ? - If not, what is next better option for me to implement this ? For e.g., - When I first Click one of the contacts like 'Brian', it saves in SharedPreferences both name and phone as Brian and 99999299999 with the first save on the Shared Preferences file 'RKS_Data' When I Click on other contact say 'Monet', my RKs_Data should appear like this: - Brian 99999299999 - Monet 00010000000 and so on.... I searched for but everywhere it is only mentioned about saving it but nothing about appending or doing some manipulations with the data... Please guide... Snippet is like below: ``` ------- public class RKsContacts_Main extends ListActivity { // private ListView listView; private List<ContactBean> list = new ArrayList<ContactBean>(); SharedPreferences sp; File Fav_Contacts_file; String contact = null; List<String> listOfFavoritePhrases = new ArrayList<String>(); @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rks_contactslist_main); ListView listview = getListView(); sp = getSharedPreferences("PACKAGE", Context.MODE_PRIVATE); ------- -------- @Override public void onClick(DialogInterface dialog, int which) { String serialized = sp.getString("phrases", null); listOfFavoritePhrases = new ArrayList<String>(Arrays.asList(TextUtils.split(serialized, ","))); // Line 141 listOfFavoritePhrases.add(name); listOfFavoritePhrases.add(phoneNo); SharedPreferences.Editor editor = sp.edit(); editor.putString("phrases",TextUtils.join(",", listOfFavoritePhrases)); editor.commit(); } }); alert.show(); } -------- ``` LogCat below: ``` 07-03 09:00:51.014: E/AndroidRuntime(9574): FATAL EXCEPTION: main 07-03 09:00:51.014: E/AndroidRuntime(9574): Process: com.example.rkscontacts_list, PID: 9574 07-03 09:00:51.014: E/AndroidRuntime(9574): java.lang.NullPointerException 07-03 09:00:51.014: E/AndroidRuntime(9574): at android.text.TextUtils.split(TextUtils.java:332) 07-03 09:00:51.014: E/AndroidRuntime(9574): at com.example.rkscontacts_list.RKsContacts_Main$4.onClick(RKsContacts_Main.java:141) 07-03 09:00:51.014: E/AndroidRuntime(9574): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 07-03 09:00:51.014: E/AndroidRuntime(9574): at android.os.Handler.dispatchMessage(Handler.java:102) 07-03 09:00:51.014: E/AndroidRuntime(9574): at android.os.Looper.loop(Looper.java:136) 07-03 09:00:51.014: E/AndroidRuntime(9574): at android.app.ActivityThread.main(ActivityThread.java:5017) 07-03 09:00:51.014: E/AndroidRuntime(9574): at java.lang.reflect.Method.invokeNative(Native Method) 07-03 09:00:51.014: E/AndroidRuntime(9574): at java.lang.reflect.Method.invoke(Method.java:515) 07-03 09:00:51.014: E/AndroidRuntime(9574): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-03 09:00:51.014: E/AndroidRuntime(9574): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-03 09:00:51.014: E/AndroidRuntime(9574): at dalvik.system.NativeStart.main(Native Method) ```