Cómo establecer el valor predeterminado de una ListPreference
Necesito establecer el valor defult para una ListPreference cuando se inicia la Actividad.
He intentado con ListPreference.setDefaultvalue("value");
pero hace que la primera entrada de la Lista sea predeterminada. Lo necesito porque debo comprobar una condición y establecer como predeterminado el valor que cumple esa condición, por lo que creo que no se puede hacer desde el archivo xml (con android:defaultValue
)
Por ejemplo, supongamos que tengo esta matriz de valores en las matrices.xml:
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
En la pantalla de preferencias xml:
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="@array/opts"
android:entryValues="@array/opts_values" />
En la Actividad me gustaría haga algo como esto:
String mycolour;
if (something) {
mycolour="1";
} else {
mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);
Pero no funciona, porque hace la primera opción por defecto. ¿Podrías explicarme cómo hacer otro por defecto? Gracias.
9 answers
¿has probado:
setValueIndex(int index);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-04 17:50:13
No necesita manejar programáticamente el valor predeterminado de ListPreferences
. Puede hacer esto en el archivo de configuración xml. A continuación se muestra un ejemplo
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="@array/opts"
android:entryValues="@array/opts_values"
android:defaultValue="2" />
Aquí seleccioné 2
como valor predeterminado. Recuerde que defaultvalue
será opts_values
elemento.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-27 19:30:07
Lo siento mi mal inglés.
- List item
- Recuperar la lista Compruebe si el valor es null. Si es null se establece en el valor predeterminado.
Código:
ListPreference dataPref = (ListPreference) findPreference("keyList");
if(dataPref.getValue() == null){
dataPref.setValueIndex(0); //set to index of your deafult value
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-06 22:20:02
O también puedes probar colour.setValue(mycolour);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-10 17:59:14
Solo para que conste si alguien más tiene este problema:
setValueIndex(int X)
está estableciendo el valor @ index X al valor predeterminado-así que es probablemente lo que está buscando.
Establezca este valor DESPUÉS DE usted agregó los Valores! (error estúpido pero me llevó media hora)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-01 08:34:52
((ListPreference) findPreference("pref_language")).setValue(Locale
.getDefault().getLanguage());
setValue()
es el método de ListPreference, y setDefaultvalue
es el método de Preference
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-07 09:49:08
En realidad es porque las preferencias de Shared persistirán después de volver a compilar la aplicación. Desinstálalo e inténtalo de nuevo.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-09 06:56:02
Este es un post antiguo, pero aquí hay otra forma de establecer el valor predeterminado para ListPreference
con la siguiente línea de código:
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-11 00:09:39
Puede establecer su valor predeterminado usando la clave como esta
<string-array name="syncFrequency">
<item name="1">Block All Calls</item>
<item name="2">Block Black List</item>
<item name="3">Block Unknown Calls</item>
<item name="4">Allow White List</item>
<item name="5">Receive All Calls</item>
</string-array>
<string-array name="syncFrequencyValues">
<item name="1">Block_All_Calls</item>
<item name="2">Block_Black_List</item>
<item name="3">Block_Unknown_Calls</item>
<item name="4">Allow_White_List</item>
<item name="5">Receive_All_Calls</item>
</string-array>
<ListPreference
android:key="prefSyncFrequency"
android:entries="@array/syncFrequency"
android:summary="%s"
android:defaultValue="Block_Black_List"
android:entryValues="@array/syncFrequencyValues"
android:title="@string/call_block_options" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-23 14:52:14