OnSharedPreferenceChangeListener not registering c

2020-08-08 08:31发布

问题:

I'm working on listpreferences and I have implemented the OnSharedPreferenceChangeListener to change the text on a textview based on which item is selected on the listpreference. However it doesn't register any changes and I have run out of ideas so any help would be appreciated. Thanks

public class pref extends PreferenceActivity implements OnSharedPreferenceChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
    setContentView(R.layout.main);

    TextView text = (TextView) findViewById(R.id.textView1);
    String keys = null;
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    pref.registerOnSharedPreferenceChangeListener(this);


    keys = pref.getString("listPref1", "");
    if (keys == "ON")
    {
        text.setText("ON");
    }
    else if (keys == "OFF")
    {
        text.setText("OFF");
    }
    else{
        text.setText("Choose One");
    }
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    //What code to I put in here to have the listener register the change
}

}

回答1:

Your code looks like it should work: you can try logging out to ensure its doing what it should. Move all the conditional code into the listener.

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    android.util.Log.v("PREFERENCE CHANGE", "[" + key + "]");
    if ("listPref1".equals(key)) {
        String keys = pref.getString("listPref1", "");
        if (keys == "ON") {
            text.setText("ON");
        } else if (keys == "OFF") {
            text.setText("OFF");
        } else {
            text.setText("Choose One");
        }
    }
}


回答2:

Your code looks good for registering to hear preference changes, you need to put some code in your onSharedPreferenceChanged method to process the preferences as they are changed. Remember that you will get called back when any preference changes, so you need to do some filtering in the method to make sure it's what you're interested in.

Could be something as simple as:

if(key.equals("listPref1"))
   text.setText("ON"); // Need to make text a class variable


回答3:

Thank you all for your help, I've sorted most of it out now here is the code

SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);

    TextView text = (TextView) findViewById(R.id.textView1);

    if (prefs.getString("listPref1", "").equals("OFF")) {
        text.setText("goodbye");
    } else if (prefs.getString("listPref1", "").equals("ON")) {
        text.setText("hello");
    }

    prefs.registerOnSharedPreferenceChangeListener(this);

}

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    TextView text = (TextView) findViewById(R.id.textView1);

    if (prefs.getString("listPref1", "").equals("OFF")) {
        text.setText("goodbye");
    } else if (prefs.getString("listPref1", "").equals("ON")) {
        text.setText("hello");
    }
}

}

As you can see I have to repeat the if/else statement, if I put it only in the listener nothing gets until a change is made and for if it's only outside the listener it only gets read when the app is first run and changes are not registered.

Isn't there a code to get the listener to notify a change has been made and the code gets re-run?



回答4:

You don't have any code to actually update your SharedPreferences. The listener will only be triggered when your pref object is edited. Example...

pref.edit().putString("listPref1", "ONE").commit();