Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SharedPreferences update does not work

I know, this issue has been dealt with in many threads, but I cannot figure out this one. So I set a shared preference like this:

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

I read the preferences like this:

SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE);
Set<String> spinnerValuesSet = null;
spinnerValuesSet = prefs.getStringSet(spinnerName,null );

Everything works, except for my changes are visible while this activity runs i.e. - I display the values from the SharedPreferences, allow the user to delete or add and then update the ListView. This works, but after I restart the application, I get the initial values. This for example is my method to delete one value from the list, update the values in SharedPreferences and update the 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 much appreciated.

like image 909
AndyZ Avatar asked Sep 13 '13 19:09

AndyZ


2 Answers

The Objects returned by the various get methods of SharedPreferences should be treated as immutable. See SharedPreferences Class Overview for reference.

You must call remove(String) through the SharedPreferences.Editor returned by SharedPreferences.edit() rather than directly on the Set returned by SharedPreferences.getStringSet(String, Set<String>).

You will need to construct a new Set of Strings containing the updated content each time since you have to remove the Set entry from SharedPreferences when you want to update its content.

like image 166
Sustain Avatar answered Nov 05 '22 05:11

Sustain


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

I solved this by created a new instance of Set and storing in 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 push the update back to SharedPreferences

like image 43
Vikas Avatar answered Nov 05 '22 06:11

Vikas