Sebbene sia in corso la documentazione, utilizzando "%s"
, è possibile visualizzare il valore selezionato come sommario, non funziona come previsto.Il testo di riepilogo ListPreference non viene aggiornato automaticamente ogni volta che si modifica la selezione
http://developer.android.com/reference/android/preference/ListPreference.html#getSummary%28%29
sto avendo il seguente preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/preference_xxx_category">
<CheckBoxPreference
android:title="@string/preference_xxx_mode_title"
android:defaultValue="false"
android:summary="@string/preference_xxx_mode_summary"
android:key="xxxModePreference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preference_yyy_category">
<ListPreference
android:title="@string/preference_yyy_mode_title"
android:defaultValue="0"
android:entries="@array/yyy"
android:entryValues="@array/yyy_values"
android:summary="%s"
android:key="yyyModePreference" />
</PreferenceCategory>
</PreferenceScreen>
ed ecco la mia arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="yyy">
<item>Display value 0</item>
<item>Display value 1</item>
</string-array>
<string-array name="yyy_values">
<item>0</item>
<item>1</item>
</string-array>
</resources>
Quello che mi aspettavo di avere il seguente codice Java per costruire una completa attività di preferenza, con una casella di controllo e una preferenza di lista, il testo di riepilogo della preferenza di lista può essere aggiornato automaticamente, quando mai eseguo la selezione. Il testo di riepilogo si alternerà tra Valore visualizzato 0 e Valore visualizzato 1.
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Tuttavia, mi rendo conto che il testo di riepilogo per la preferenza dell'elenco non si aggiornerà automaticamente. Solo quando eseguo il segno di spunta/deseleziona sulla casella di controllo, solo l'intera attività sarà invalidata e il testo di riepilogo per la preferenza dell'elenco verrà aggiornato SOLO.
Quindi, devo rivedere il mio codice come segue, che sembra più ingombrante.
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onResume() {
super.onResume();
// Cumbersome way to make sure list preference's summary text is being updated.
final String key = "yyyModePreference";
ListPreference listPreference = (ListPreference)findPreference(key);
final String value = PreferenceManager.getDefaultSharedPreferences(this).getString(key, key);
final int index = listPreference.findIndexOfValue(value);
if (index >= 0) {
final String summary = (String)listPreference.getEntries()[index];
listPreference.setSummary(summary);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences options, String key) {
// Cumbersome way to make sure list preference's summary text is being updated.
if (key.equals("yyyModePreference")) {
ListPreference listPreference = (ListPreference)findPreference(key);
final String value = options.getString(key, key);
final int index = listPreference.findIndexOfValue(value);
if (index >= 0) {
final String summary = (String)listPreference.getEntries()[index];
listPreference.setSummary(summary);
}
}
}
}
È un modo corretto? Esiste un modo semplificato per garantire che il testo di riepilogo di ListPreference venga sempre aggiornato automaticamente ogni volta che si verifica un cambiamento nella selezione.
Grazie, mi ci sarebbe voluto molto tempo per capirlo. – Gerhard
possibile duplicato di [Modifica il riepilogo di un ListPreference con il nuovo valore (Android)] (http://stackoverflow.com/questions/7017082/change-the-summary-of-a-listpreference-with-the-new- value-android) – CAMOBAP