Android: ¿cómo llamo a findPreference () desde mi actividad principal?

I'm handling the preferences screen for my Android app. I want to disable (grey it out) an item if the preceding one has a specific value.

I have implemented two classes: MainActivity and PreferencesActivity.

In MainActivity I do:

public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
    public void onCreate (Bundle savedInstanceState) {
        sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

Then, when my preferences are requested by the user:

startActivity(new Intent(this, PreferencesActivity.class));

Then, to handle preferences in MainActivity, I do:

public void onSharedPreferenceChanged (SharedPreferences prefs, String key) {
    if ("sport".equals(key)) {
        sport = prefs.getString("sport", "");
        ListPreference lp = findPreference("match_duration");
        if (sport.equals(getString(R.string."sport_soccer"))) {

The problem is I can't call findPreference in MainActivity ("The method findPreference(String) is undefined for the type MainActivity"...). Is my approach wrong? Shoud I implement onSharedPreferenceChanged() method in PreferencesActivity? If so, how do I use MainActivity properties in PreferencesActivity?

preguntado el 10 de marzo de 12 a las 12:03

1 Respuestas

findPreference() should be called from a class implementing PreferenceActivity interfaz (PreferencesActivity en mi contexto). MainActivity properties can be accessed via SharedPreferences clase.

respondido 28 nov., 17:09

Could you share the code changes you did to solve the issue? I am not exactly getting how did you implement it. - imposible

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.