2

I'm writing an android app with a preferencesActivity in which selections made in my instance of preferencesActivity affect the values of other preferences items displayed. While I'm able to change the values of the underlying SharedPreferences items pogrammatically, those changed values aren't reflected in the displayed list items until I exit my preferencesActivity and reload it. Below is a stripped down version of my settings class and xml file which illustrate the problem. If a user sets Guitar as the value for the preference with the key instrumentList, I'd like the preference with key tuningChoice to revert to Standard.

//necessary import declarations go here
public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        app_preferences.registerOnSharedPreferenceChangeListener(this);
    }

    public void  onSharedPreferenceChanged  (SharedPreferences  sharedPreferences, String  key)   {
        Log.d("onSharedPreferencesChanged", "sharedPreferences changed.  key: " + key);
        Editor preferencesMod = sharedPreferences.edit();
        String instrumentChoice = sharedPreferences.getString("instrumentList", "Guitar");
        if(key.equals("instrumentList")) {
            Log.d("Settings", "key is instrumentList.  chooseTuning before if: " +    sharedPreferences.getString("chooseTuning", "no luck"));
            if(instrumentChoice.equals("Guitar")) {
                preferencesMod.putString("chooseTuning", "Standard");
                preferencesMod.commit(); 
                Log.d("Settings", "chooseTuning after if: " +      sharedPreferences.getString("chooseTuning", "ciao"));
            }
        }
    }
}

xml file preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="Settings">
  <ListPreference android:title="Choose an Instrument" android:key="instrumentList" android:entryValues="@array/instruments" android:entries="@array/instruments"/>
        <ListPreference android:title="Choose Tuning" android:key="chooseTuning" android:entryValues="@array/tuningChoices" android:entries="@array/tuningChoices" android:persistent="true"/>
</PreferenceScreen>

I can call addPreferencesFromResource again in my onSharedPreferenceChanged method and that loads a duplicate of all the preferences items, displayed below the old items, with the correct values. If I could figure out some way to cancel out the initial addPreferencesFromResource called during onCreate, I guess I would be set.

Any help would be appreciated, Thanks

dave.c
  • 10,910
  • 5
  • 39
  • 62
vancan1ty
  • 563
  • 1
  • 4
  • 16
  • Check the answer of Joseph here. http://stackoverflow.com/questions/8003098/how-do-you-refresh-preferenceactivity-to-show-changes-in-the-settings – Yefei Apr 17 '12 at 02:55

4 Answers4

1

I do something along these lines...hopefully it helps:

    ListPreference list = (ListPreference) getPreferenceManager().findPreference("myList");
    list.setValue(sharedPrefs.getString("myList", "default"));
    list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            sharedPrefs.put("myList", newValue.toString());
            return true;
        }

    });
Nick
  • 6,375
  • 5
  • 36
  • 53
  • ^This looks promising, thanks. One quick question: what is sharedPrefs in this code snippet? Is it an instance of SharedPreferences? because SharedPreferences doesn't have a put method... – vancan1ty Feb 13 '12 at 15:47
  • sharedPrefs is my own custom wrapper around SharedPreferences. You could just use the normal SharedPreferences class...just make sure to use commit() where necessary. – Nick Feb 13 '12 at 17:01
0

If anyone comes to this problem, this is the solution:

ListView list = preferenceActivity.getListView();
list.performItemClick(list, 1, list.getItemIdAtPosition(1));
htafoya
  • 18,261
  • 11
  • 80
  • 104
0

Maybe I am too late for answering this. But, I hope this might help beginner like me.

PackageInfo packageInfo = null;
try {
    packageInfo = preference.getContext().getPackageManager().getPackageInfo(preference.getContext().getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();   

}
preference.setSummary(packageInfo.versionName);
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
Rudy Rudy
  • 125
  • 1
  • 6
0

You need to prevent addPReferencesFromResource from running twice? Is this loading your default values? If so, add an additional SharedPreference called DEFAULTS_LOADED and read its value in on create like: (WARNING PSUEDO CODE):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean loadDefaults = app_preferences.getBoolean(DEFAULTS_LOADED, true);
    if(loadDefaults)
    {  
        addPreferencesFromResource(R.xml.preferences);
        Editor editor = app_preferences.edit();
        editor.putBoolean(DEFAULTS_LOADED, true);
        editor.commit();
    } 
    app_preferences.registerOnSharedPreferenceChangeListener(this);
}

This will prevent you defaults from being written to the shared preferences every time your activity starts. I assume this is at least a portion of your issue.

Nick Campion
  • 10,479
  • 3
  • 44
  • 58
  • no, that's not my problem. addPreferencesFromResource currently only gets called during onCreate. IF I call addPreferencesFromResource again after I change the sharedPreferences values, it loads a duplicate set of preferences with the CORRECT values below the old set of preferences with the INCORRECT values. – vancan1ty Feb 13 '12 at 15:21
  • to get around this, I either need to make the initial set of preferences update somehow or clear the initial set and replace them with the new set with the correct values. I can't figure out a way to do either. – vancan1ty Feb 13 '12 at 15:22
  • also, addPreferencesFromResource does not load "default" values persay but rather whatever was saved the last time the app was used. ^Thanks for the idea though. – vancan1ty Feb 13 '12 at 15:31
  • I'm confused on how what you are doing is happening. Regardless, calling clear on the editor will remove all existing properties in the shared perefences. – Nick Campion Feb 13 '12 at 15:31
  • 1
    I've tried that. The behavior I get is that the underlying sharedPreferences items do all get cleared BUT the preferences displayed in the UI remain the same until I leave the activity and come back. – vancan1ty Feb 13 '12 at 15:35