0

I have two onclick actions that modifies a shared preference file :

int value = counters.getInt("wcounter", 0);
wcounter = value;
wcounter++;
Editor editor = counters.edit();
editor.putInt("wcounter", wcounter);
editor.commit();

and

int value = counters.getInt("wcounter", 0);
wcounter = value;
wcounter--;
Editor editor = counters.edit();
editor.putInt("wcounter", wcounter);
editor.commit();

but when the first one is re executed it takes it's old value.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Lou
  • 117
  • 1
  • 8
  • Not sure but instead of using `editor.commit();`, try using [editor.apply();](http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply%28%29). See http://stackoverflow.com/questions/10186215/sharedpreferences-value-is-not-updated – Shobhit Puri Aug 25 '13 at 21:05
  • yes but it requires a level of APU of at least 9 while I try to work with 7... – Lou Aug 25 '13 at 21:10
  • okay. But just curious that it did work or not? – Shobhit Puri Aug 25 '13 at 21:13
  • no it didin't change a thing... – Lou Aug 25 '13 at 21:31
  • okay. So, in start first one take value `1` and saves it. Then if you keep pressing the add button it is giving just `1` again and again? Also can you show where are you declaring the SharedPreference ? – Shobhit Puri Aug 25 '13 at 21:43
  • no when the upper one is clicked it increments correctly (for exemple to 20) then if the lower one is clicked it decrements (for exemple to 10) but when the upper one is clicked again it starts to increments from 20 where it should do it from 10. this is how i first declare it 'final SharedPreferences counters = getSharedPreferences("counters", 0);' – Lou Aug 25 '13 at 22:52

1 Answers1

0

Well my bad, I had a double function that was doing the same thing except the second one was not writing it to the file... Thanks everybody for your help

Lou
  • 117
  • 1
  • 8