Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SharedPreferences update does not work
    text
    copied!<p>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:</p> <pre><code>SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putStringSet(spinnerName, myValueSet ); editor.apply(); </code></pre> <p>I read the preferences like this:</p> <pre><code>SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); Set&lt;String&gt; spinnerValuesSet = null; spinnerValuesSet = prefs.getStringSet(spinnerName,null ); </code></pre> <p>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</p> <pre><code>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&lt;String&gt; 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(); } }); </code></pre> <p>And this is the method that updates the ListView:</p> <pre><code> private void updateListValues() { SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE); Set&lt;String&gt; spinnerValuesSet = prefs.getStringSet(spinnerName,null ); if(spinnerValuesSet.size() &gt; 0) { names = new ArrayList&lt;String&gt;(); names.clear(); int k=0; for (String s : spinnerValuesSet) { names.add(k, s); k++; } namesAA = new ArrayAdapter&lt;String&gt; ( this, android.R.layout.simple_list_item_activated_1, names ); myList.setAdapter(namesAA); } </code></pre> <p>}</p> <p>Any help is much appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload