0

I have a list preference with two values and I want to update these two values from with values from another array.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
     Resources resources = this.getResources();
     String languageData = prefs.getString("languageAlias", " ");
     String[] languageAlias = resources.getStringArray(R.array.languageAlias);
     String[] voiceData = resources.getStringArray(R.array.voiceData);

     int a = 0;
     for(a=0; a<languageAlias.length; a++){
     if(languageData.equals(languageAlias[a]))
     {
         //this is where I have problems
         prefs.edit().putString("voiceAlias", voiceData[2*a]);
         prefs.edit().commit();
         break;
     }

I have been able to get it working up until I have to use the puString command to make the change and commit. Also how do I specify which item in the list preference I want to change, as all I am required to pass to the putString function is a key?

Amanni
  • 1,924
  • 6
  • 31
  • 51

2 Answers2

0

After this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

you need to add:

SharedPreferences.Editor editor = prefs.edit();

Then instead of

prefs.edit().putString("voiceAlias", voiceData[2*a]);
prefs.edit().commit();

use

editor.putString("voiceAlias", voiceData[2*a]);
editor.commit();

The documentation on edit() says:

Create a new Editor for these preferences

This means that each time you call prefs.edit() it creates a new Editor object, so when you put string with prefs.edit().putString(...) and when you commit with prefs.edit().commit() you're referencing two new, different Editor objects.

I believe you can also do prefs.edit().putString(...).commit(), but I am not sure if that is possible.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • This works but doesn't give me the result in the way I want it. I want to update both items. This updates just one – Amanni Mar 29 '12 at 12:50
  • If you know you are only going to have two different things to add, then you can have one key named `voiceAlias` and another key with a different name. If you have an indefinite number of things to add to one... heading, per-say, then you should use an `sqlite` database, I believe. I don't know how to do that. – Reed Mar 30 '12 at 07:22
0

You cannot write String arrays to your SharedPreferences. You need to change that. You can use

putStringSet (String key, Set values)

but this is available only from API Level 11 onwards. So check that. Or you could convert your array into a single String or into a JSON String (an example I saw elsewhere)

Check out the link below. Is it possible to add an array or object to SharedPreferences on Android

Community
  • 1
  • 1
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • I read about the putStringSet function but my API level is too low – Amanni Mar 29 '12 at 12:52
  • In that case I suggest you change it to some other format like JSON/XMLor just a plain String with some delimiter like "|". You can then load it and do a splitString to get the array back :) – Shubhayu Mar 29 '12 at 12:56
  • Are you able to write a String directly into the Preferences? That kinda sounds questionable. – Shubhayu Mar 29 '12 at 12:57
  • yes, I can write a string directly into the preference but it doesn't give the desired result – Amanni Mar 30 '12 at 15:06
  • Why dont u try te suggestion I mentioned? It might work for you. – Shubhayu Mar 30 '12 at 16:20