Android SharedPreferences update not working

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.

+6
source share
5 answers

Objects returned by various get SharedPreferences methods should be treated as immutable. For help, see Overview of the Common Partition Class .

You should call remove(String) via SharedPreferences.Editor returned by SharedPreferences.edit() , and not directly into the Set returned by SharedPreferences.getStringSet(String, Set<String>) .

You will need to create a new set of rows each time containing the updated content, since you need to remove the Set record from SharedPreferences when you want to update its contents.

+9
source

The problem arises because the Set returned by the SharedPreference is immutable. https://code.google.com/p/android/issues/detail?id=27801

I solved this by creating a new instance of Set and storing all the values ​​returned from SharedPreferences.

  //Set<String> address_ids = ids from Shared Preferences... //Create a new instance and store everything there. Set<String> all_address_ids = new HashSet<String>(); all_address_ids.addAll(address_ids); 

Now use the new instance to return the update to SharedPreferences

+2
source

Maybe I'm wrong, but I think the problem is that you get the general settings - this is the problem. Try using

SharedPreferences prefs = getSharedPreferences("appPreferenceKey", Context.Mode_Private);

0
source

Use editor.commit(); instead of editor.apply();

Code example:

 SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putStringSet(spinnerName, myValueSet ); editor.commit(); 


Hope this helps.

0
source

Depending on your OS build, you may have to save the values ​​differently.

 boolean apply = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD; public static void saveValue(SharedPreferences.Editor editor) { if(apply) { editor.apply(); } else { editor.commit(); } } 
0
source

Source: https://habr.com/ru/post/953799/


All Articles