I have a project that I am building in 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. However, the defaultValue is not working whether I add it via Java or XML. 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
<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
/**
* 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
<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. This means that lp.setEntryValues(values)
works and that values
is a properly formatted Set. However, lp.setDefaultValue(values)
has no effect. 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. 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.