Domanda

Ho provato a impostare il valore predefinito per ListPreference ma non viene visualizzato nulla.

Puoi controllare il mio codice per eventuali errori?

Grazie.

Davvero, Emad

Questo è nel file settings.xml:

<PreferenceCategory android:title="Media:">
    <CheckBoxPreference android:key="ChimeWhenMusicIsPlaying"
        android:title="@string/ChimeWhenMusicIsPlayingTitle" android:summary="@string/ChimeWhenMusicIsPlayingSummary"
        android:defaultValue="false" />

    <ListPreference android:title="Chime Volume"
        android:key="ChimeVolume" android:summary="Select volume for the chiming sound."
        android:entries="@array/chimeVolumeLabels" android:entryValues="@array/chimeVolumeValues"
        android:defaultValue="1" />

</PreferenceCategory>

Questo è nel file degli array:

<resources>

    <string-array name="chimeVolumeLabels">
    <item>Default</item>
    <item>Soft</item>
    <item>Medium</item>
    <item>Loud</item>
    </string-array>

    <string-array name="chimeVolumeValues">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    </string-array>
</resources>
È stato utile?

Soluzione

Ho scoperto che a volte ho bisogno di cancellare i dati dell'applicazione. Disinstallare e reinstallare l'app. Dopodiché, tutto funziona come previsto.

Altri suggerimenti

Ho scoperto che devo chiamare PreferenceManager.setDefaultValues() nella mia attività Preferenze affinché il mio valore predefinito venga visualizzato inizialmente.

public class PreferencesActivity extends PreferenceActivity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This static call will reset default values only on the first ever read
        PreferenceManager.setDefaultValues(getBaseContext(), R.xml.settings, false);

        addPreferencesFromResource(R.xml.settings);
    }
}
index = listPreference.findIndexOfValue(listPreference.value)
listPreference.setValueIndex(index)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top