0

I'm trying to save a string in preference by adding it to the editor when a user press a button. Then i'm trying to retreieve the strings from the preference and turn it into an arrayList.

in onCreate

 this.context = getApplicationContext();
        SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

         int size = updatableList.size();
         editor.putInt("list_size", size);

         for (int i = 0; i < size; i++) {
             ((SharedPreferences) editor).getString("list_"+i, updatableList.get(i));
         }       
         editor.commit();

Later in the application

               updatableList.add(picturePath);
               i=i++;   
               //saving path to preference********
             SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
             ((Editor) editor).putString("list_"+i, picturePath);
             editor.commit();

It says the prefs later in the application is unused which i think is odd because i thought it told it to putString. The application crashes when it gets to there. Why does my prefs later in the application get used?

Cat
  • 66,919
  • 24
  • 133
  • 141
user2872261
  • 151
  • 1
  • 2
  • 7
  • Also did i use putStrings and getStrings correct? It's my first time using it and also using preference. thanks in avd – user2872261 Oct 24 '13 at 00:44

3 Answers3

0

Are you trying to getString or putString here

for (int i = 0; i < size; i++) {
         ((SharedPreferences) editor).getString("list_"+i, updatableList.get(i));
     } 

Cos the code before this, does a putInt.

SharedPreference works like a key value pair. If the key is not found, it returns the default value.

From the above code, you are first trying to getString and later trying to putString. So the initial get will return default value.

Also, you have mentioned that the code crashes. Do you mind pasting the crash log.

prijupaul
  • 2,076
  • 2
  • 15
  • 17
0

I see a main problem here and it's the following line:

i=i++;

This will not change i at all. You're telling it to increment i but at the same time setting i to the value prior to incrementing it. i++; is what you want.

Also I'd suggest using SharedPreferences.Editor.putStringSet() instead of putString() - that way you don't need to care about size and so on.

ddmps
  • 4,350
  • 1
  • 19
  • 34
  • Ok, i just tired it. But now its in a set. How do i extract it? Set set = new HashSet(); set = prefs.getStringSet("key", null) – user2872261 Oct 24 '13 at 04:39
0

In saving pref try it like this snippet

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.putString("KEY", "value");
edit.commit();

And to fetch it

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.getString("KEY", "defaultValue");
RussVirtuoso
  • 900
  • 1
  • 9
  • 20
  • Thanks for replaying Russ. can you explain the fetch process some more? I tired this but SP is a sharedpreference so i can't use it in my string methods. I'm not sure how to get my save string. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); sp.getString("KEY", imagePath); – user2872261 Oct 24 '13 at 04:26