How to set the Default Value of a ListPreference

2019-01-18 08:15发布

i need to set the defult value for a ListPreference when the Activity starts. I've tried with ListPreference.setDefaultvalue("value"); but it makes the firts Entry of the List as default. I need it because i must check a condition and set as default the value which satisfies that condition, so I think it can't be done from the xml file (with android:defaultValue)

For example, suppose I have this array of values in the arrays.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>

In the PreferenceScreen xml:

<ListPreference
    android:title="Colour select"
    android:summary="Select your favourite"
    android:key="colour"
    android:entries="@array/opts"
    android:entryValues="@array/opts_values" />

In the Activity I'd like to do something like this:

String mycolour;
if (something) {
    mycolour="1";
} else {
    mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);

But it doesn't work, because it makes the first choice as default. Could you explain me how to make another one as default? Thanks.

10条回答
爷、活的狠高调
2楼-- · 2019-01-18 08:39

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

查看更多
SAY GOODBYE
3楼-- · 2019-01-18 08:40

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" />
查看更多
来,给爷笑一个
4楼-- · 2019-01-18 08:42

Have you tried:

setValueIndex(int index);
查看更多
太酷不给撩
5楼-- · 2019-01-18 08:42

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);
查看更多
来,给爷笑一个
6楼-- · 2019-01-18 08:43

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
}
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-18 08:47

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.

查看更多
登录 后发表回答