0

I've read everything and i thought i understood it but something just isnt working in my code and I'm really not sure why, can anyone help me please? my settings activity has 3 switches i want to square them up against 3 shared preferences booleans, so my activity extends PreferenceActivity and implements SharedPreferences.OnSharedPreferenceChangeListener my switches are initialized in onCreate and then i have overriden onSharedPreferenceChanged later in my activity

ujm = (SwitchPreference) findPreference(getString(R.string.ujmkey));
totaljobs = (SwitchPreference) findPreference(getString(R.string.totaljobskey));
indeed = (SwitchPreference) findPreference(getString(R.string.indeedkey));



@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals("ujmkey")) {
        boolean ujmbool = sharedPreferences.getBoolean("ujmkey", false);
        //Do whatever you want here. This is an example.
        if (ujmbool) {
            ujm.setSummary("Enabled");
            ujm.setChecked(true);
        } else {
            ujm.setSummary("Disabled");
            ujm.setChecked(false);
        }
    }
    if (key.equals("totaljobskey")) {
        boolean totaljobsbool = sharedPreferences.getBoolean("totaljobskey", false);
        //Do whatever you want here. This is an example.
        if (totaljobsbool) {
            totaljobs.setSummary("Enabled");
            totaljobs.setChecked(true);
        } else {
            totaljobs.setSummary("Disabled");
            totaljobs.setChecked(true);
        }
    }
    if (key.equals("indeedkey")) {
        boolean indeedbool = sharedPreferences.getBoolean("indeedkey", false);
        //Do whatever you want here. This is an example.
        if (indeedbool) {
            indeed.setSummary("Enabled");
            indeed.setChecked(true);
        } else {
            indeed.setSummary("Disabled");
            indeed.setChecked(true);
        }
    }
}

this code updates the first switch summary but none of the others and doesnt update any shared preferences (i thought this happened automatically) the shared preferences keys are definitely correct, maybe its an issue with my xml ill include that here

<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="Inputs" >
<SwitchPreference
    android:title="@string/ujm"
    android:key="@string/ujmkey"/>
<SwitchPreference
    android:title="@string/totaljobs"
    android:key="@string/totaljobskey"/>
<SwitchPreference
    android:title="@string/indeed"
    android:key="@string/indeedkey"/>
</PreferenceCategory>

<PreferenceCategory
    android:title="Data" >

<Preference android:title="Delete"
    android:key="@string/pref_delete"
    android:summary="Deletes all saved data"/>

</PreferenceCategory>

<PreferenceCategory android:title="FAQ" >

    <Preference android:title="About"
    android:summary="About the app"
    android:key="about"/>

    <Preference
    android:title="Share"
    android:summary="Share the app with friends"
    android:key="share"/>

</PreferenceCategory>

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
martinseal1987
  • 1,862
  • 8
  • 44
  • 77

0 Answers0