OnSharedPreferenceChangeListener not registering change in preference
-
29-10-2019 - |
Question
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
}
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow