简体   繁体   English

如何设置 ListPreference 的默认值

[英]How to set the Default Value of a ListPreference

i need to set the defult value for a ListPreference when the Activity starts.我需要在 Activity 启动时为 ListPreference 设置默认值。 I've tried with ListPreference.setDefaultvalue("value");我试过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 )我需要它,因为我必须检查一个条件并将满足该条件的值设置为默认值,所以我认为它不能从 xml 文件中完成(使用android:defaultValue

For example, suppose I have this array of values in the arrays.xml:例如,假设我在 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:在 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.谢谢。

You don't need to programmatically handle the default value of ListPreferences .您不需要以编程方式处理ListPreferences的默认值。 You can do this in xml setting file.您可以在 xml 设置文件中执行此操作。 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.这里我选择了2作为默认值。 Remember defaultvalue will be opts_values element.记住defaultvalue将是opts_values元素。

你有没有尝试过:

setValueIndex(int index);

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
}

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

或者你也可以试试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 . setValueIndex(int X)正在将值setValueIndex(int X)设置为默认值 - 所以可能是你正在寻找的

Set this value AFTER you added the Values!添加值设置此值! (stupid mistake but took me half an hour) (愚蠢的错误,但花了我半小时)

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

setValue() is ListPreference's method, and setDefaultvalue is Preference's method setValue()是 ListPreference 的方法, setDefaultvalue是 Preference 的方法

Actually it's because the SharedPreferences will persist after you re-build the app.实际上这是因为在您重新构建应用程序后 SharedPreferences 将持续存在。 Uninstall it and try again.卸载它并重试。

这是一篇旧帖子,但这是使用以下代码行设置ListPreference的默认值的另一种方法:

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

Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.使用列表标签中的 xml 属性android:defaultValue="<VALUE>"来设置默认值。

Note: the <VALUE> is the actual value and not the index of string array.注意: <VALUE>是实际值,而不是字符串数组的索引。

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.奇怪,我知道,但它在我的情况下有效。

If you are using Android Jetpack Preference, known as androidx.preference:preference-ktx:1.1.1 in Kotlin, you can use:如果您使用的是 Android Jetpack Preference,在 Kotlin 中称为androidx.preference:preference-ktx:1.1.1 ,您可以使用:

app:defaultValue="<Value_in_string-array_with_values>" . app:defaultValue="<Value_in_string-array_with_values>"

Additionally: defaultValue is not an index number, is the actual value from the values array.另外: defaultValue不是索引号,是 values 数组中的实际值。

I also recommend using a string resource for the default value and clearing the data, uninstalling the app , or removing the file:我还建议使用字符串资源作为默认值并清除数据、卸载应用程序或删除文件:

  • <package_name_in_app_manifest>_preferences.xml in data/data/shared_prefs/<package_name_in_app_manifest> . <package_name_in_app_manifest>_preferences.xmldata/data/shared_prefs/<package_name_in_app_manifest>中。 Replace <package_name_in_app_manifest> with real name like com.example.yourapp .<package_name_in_app_manifest>替换为真实姓名,如com.example.yourapp

I was having the same issue and the defaultValue wasn't updating because it already had a wrong default value of "true".我遇到了同样的问题,并且defaultValue没有更新,因为它已经有一个错误的默认值“true”。 Solved it by using the Android Studio File Explorer and deleting the file.通过使用 Android Studio 文件资源管理器并删除文件来解决它。

And here is my solution example:这是我的解决方案示例:

res/xml/root_preferences.xml

<ListPreference
    
    app:key="mic"
    
    app:title="@string/select_mic_title"
 
    app:useSimpleSummaryProvider="true"
   
    app:entries="@array/mic_entries"

    app:entryValues="@array/mic_values"
    app:defaultValue="@string/default_mic"
    />

res/values/arrays

<!-- Mic Preference -->

<string-array name="mic_entries">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>


<string-array name="mic_values">

    <item>@string/default_mic</item>

    <item>Headband</item>

</string-array>

res/strings.xml

<string name="default_mic">Integrated</string>

Result: Setting Fragment with default list value of "Integrated" given by string resource @string/default_mic结果:使用字符串资源@string/default_mic 给出的默认列表值“Integrated”设置片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM