简体   繁体   English

MultiSelectListPreference defaultValues不起作用

[英]MultiSelectListPreference defaultValues not working

I have a project that I am building in Android Studio. 我有一个我在Android Studio中构建的项目。 I've added the prebuilt preference screen with fragments which compiles and runs fine. 我添加了预构建的首选项屏幕,其中包含编译并运行正常的片段。 I added a MultiSelectListPreference to one of the preference screens which displays fine and stores preference settings. 我在其中一个首选项屏幕上添加了一个MultiSelectListPreference,它显示正常并存储首选项设置。 However, the defaultValue is not working whether I add it via Java or XML. 但是,无论是通过Java还是XML添加,defaultValue都不起作用。 I have read the scores of other questions about how to do this. 我已经阅读了有关如何执行此操作的其他问题。 I know how. 我知道该怎样。 My question is what could cause it not to work as intended? 我的问题是什么可能导致它无法按预期工作?

pref_general.xml pref_general.xml

<MultiSelectListPreference
    android:key="@string/pref_key_starting_addresses"
    android:summary="@string/pref_description_addresses"
    android:title="@string/pref_title_addresses"
    android:entries="@array/empty_array"
    android:entryValues="@array/empty_array" />

PreferenceActivity.java PreferenceActivity.java

/**
 * This method autopopulates a MultiSelectListPreference with array values
 * loaded from XML.
 */
private void populateMultiSelectListPreference() {
    List<TypedArray> origins = ResourceHelper.getMultiTypedArray(mContext, "origins");
    CharSequence[] entries = new CharSequence[origins.size()];
    CharSequence[] values = new CharSequence[origins.size()];
    int counter = 0;
    for (TypedArray item : origins) {
        entries[counter] = item.getString(0);
        values[counter] = String.valueOf(counter);
        counter++;
    }

    final MultiSelectListPreference lp = (MultiSelectListPreference) findPreference(getString(R.string.pref_key_starting_addresses));
    lp.setEntries(entries);
    lp.setDefaultValue(values);
    lp.setEntryValues(values);
}

arrays.xml arrays.xml

<string-array name="empty_array" />

<!-- a number of these are used to generate the MultiSelectListPreference
     titles and entry values-->
<array name="origins_0">
    <item>Text used for title</item>
    <item>data 1</item>
    <item>data 2</item>
</array>

I know that everything is working properly because debugging shows that values contains a valid Set and saving preferences works fine. 我知道一切正常,因为调试显示values包含有效的Set并且保存首选项工作正常。 This means that lp.setEntryValues(values) works and that values is a properly formatted Set. 这意味着lp.setEntryValues(values)可以正常工作,并且该values是一个格式正确的Set。 However, lp.setDefaultValue(values) has no effect. 但是, lp.setDefaultValue(values)无效。 The checkboxes are all unchecked at first run. 首次运行时,所有复选框都未选中。 My goal is to have them all selected by default. 我的目标是默认选中它们。

Yes, I have used the Clean and rerun 'app' command from Android Studio's Run menu between tests. 是的,我在测试之间使用了Android Studio的Run菜单中的Clean and rerun 'app'命令。 Additionally, I've cleared the app cache manually on the device. 此外,我已在设备上手动清除了应用缓存。

[EDIT] [编辑]

With suggestions in comments, I have also tried the following modification. 根据评论中的建议,我也尝试了以下修改。

private void populateMultiSelectListPreference() {
    List<TypedArray> origins = ResourceHelper.getMultiTypedArray(mContext, "origins");
    CharSequence[] entries = new CharSequence[origins.size()];
    CharSequence[] values = new CharSequence[origins.size()];
    Set<String> defaults = new HashSet<>();
    int counter = 0;
    for (TypedArray item : origins) {
        entries[counter] = item.getString(0);
        values[counter] = String.valueOf(counter);
        defaults.add(String.valueOf(counter));
        counter++;
    }

    final MultiSelectListPreference lp = (MultiSelectListPreference) findPreference(getString(R.string.pref_key_starting_addresses));
    lp.setEntries(entries);
    lp.setEntryValues(values);
    lp.setDefaultValue(defaults);
}

A quick guess here but I wonder if the reason is just the order you are calling your values, I would set the default values in the last step considering the full list should be initialized first: 这里有一个快速的猜测,但我想知道原因是否只是你调用你的值的顺序,我会在最后一步设置默认值,考虑到应该首先初始化完整列表:

final MultiSelectListPreference lp = (MultiSelectListPreference) findPreference(getString(R.string.pref_key_starting_addresses));
lp.setEntries(entries);
lp.setEntryValues(values);
lp.setDefaultValue(values);

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

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