Domanda

Sto lavorando a ListPreferences e ho implementato OnsharedPreferenceChangelistener per modificare il testo su un TextView in base a quale elemento è selezionato su ListPreference. Tuttavia, non registra alcun cambiamento e ho esaurito le idee, quindi qualsiasi aiuto sarebbe apprezzato. Grazie

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
}

}

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top