I know this problem has been addressed in many threads, but I cannot figure it out. Therefore, I set my general preference as follows:
SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putStringSet(spinnerName, myValueSet ); editor.apply();
I read settings like this:
SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); Set<String> spinnerValuesSet = null; spinnerValuesSet = prefs.getStringSet(spinnerName,null );
Everything works, except that my changes are visible during this action. i. I display values from SharedPreferences, allow the user to delete or add, and then update the ListView. This works, but after restarting the application, I get the initial values. This, for example, is my method for removing a single value from a list, updating values in SharedPreferences, and updating ListView
Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); Set<String> spinnerValuesSet = prefs.getStringSet(spinnerName,null ); for (String s : spinnerValuesSet) { if(s == currentSelectedItemString) { spinnerValuesSet.remove(s); SharedPreferences.Editor editor = prefs.edit(); editor.putStringSet(spinnerName, spinnerValuesSet ); editor.apply(); break; } } updateListValues(); } });
And this is the method that updates the ListView:
private void updateListValues() { SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); Set<String> spinnerValuesSet = prefs.getStringSet(spinnerName,null ); if(spinnerValuesSet.size() > 0) { names = new ArrayList<String>(); names.clear(); int k=0; for (String s : spinnerValuesSet) { names.add(k, s); k++; } namesAA = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_activated_1, names ); myList.setAdapter(namesAA); }
}
Any help is greatly appreciated.