How to set the Default Value of a ListPreference

£可爱£侵袭症+ 提交于 2019-11-28 09:35:05

Have you tried:

setValueIndex(int index);
Md. Naushad Alam

You don't need to programatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example

   <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" />

here i selected 2 as a default value. Remember defaultvalue will be opts_values element.

Sorry my bad English.

  1. List item
  2. Retrieve the list Check if the value is null. If it is null set to the default value.

Code:

ListPreference dataPref = (ListPreference) findPreference("keyList");

if(dataPref.getValue() == null){
    dataPref.setValueIndex(0); //set to index of your deafult value
}
ungalcrys

or you can also try colour.setValue(mycolour);

Just for the record if someone else has this problem:

setValueIndex(int X) is setting the value @ index X to the default value - so it is probably what you are looking for.

Set this value AFTER you added the Values! (stupid mistake but took me half an hour)

laomo
((ListPreference) findPreference("pref_language")).setValue(Locale
                .getDefault().getLanguage());

setValue() is ListPreference's method, and setDefaultvalue is Preference's method

Actually it's because the SharedPreferences will persist after you re-build the app. Uninstall it and try again.

This is an old post, but here's another way to set the default value for ListPreference with the following line of code:

PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);

You can set your default value by using the key like this

<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" />

Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.

Note: the <VALUE> is the actual value and not the index of string array.

If still its not working, try below steps.

  • Clear application data.
  • Uninstall and reinstall the app
  • Check the list preference, you will see the default value selected

Strange, I know but it worked in my case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!