MultiSelectList preference不存储价值? [英] MultiSelectListPreference not storing values?
问题描述
我是相当新的Android应用程序开发,所以也许我只是做一个简单的新手的错误,但这里是我的问题: 我有2个简单的活动,MainActivity和SettingsActivity。在MainActivity我有一个按钮,显示SettingsActivity。在SettingsActivity我包括preferenceFragment SettingsFragment,并在活动的底部显示一个按钮栏。内SettingsFragment我有一个MultiSelectList preference定义如下:
I'm rather new to Android App developing so maybe I'm just making a simple newbie mistake, but here's my problem: I have 2 simple Activities, MainActivity and SettingsActivity. In MainActivity I have a button which displays SettingsActivity. Within SettingsActivity I include a PreferenceFragment SettingsFragment and display a ButtonBar at the bottom of the Activity. Within the SettingsFragment I have a MultiSelectListPreference defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/title_schedule_settings">
<MultiSelectListPreference
android:key="@string/key_list_schedule"
android:title="@string/title_schedule_list"
android:dialogTitle="@string/title_schedule_list"
android:entries="@array/list_weekdays"
android:entryValues="@array/list_weekdays"
android:defaultValue="@array/empty_list"
android:persistent="true"/>
</PreferenceCategory>
</PreferenceScreen>
现在,当我选择了preference它让我看到列表中的所有条目的数组中定义的,我可以选择多个条目,当我确认对话框中的值实际上存储在共享$ P $定义项下pferences。但是,如果我现在再次显示preference它会告诉我,选择pviously选定项目$ P $,但值不再存储在共享preferences内,经过一番摆弄周围,我不得不意识到,在共享preferences价值观显然得到尽快所示的对话框中抹去。
Now when I select that Preference it shows me the list with all the entries as defined in the array, I can select multiple entries and when I confirm the dialog the values are in fact stored in the SharedPreferences under the defined key. But if I now show the Preference again it will show me previously selected items as selected, but the values are no longer stored within the SharedPreferences, and after some fiddling around I had to realize that the values in the SharedPreferences apparently get wiped as soon as the dialog is shown.
所以,现在我的问题是:这是正常/预期的行为或者这是一个错误?我怎样才能解决此问题? 我已经尝试过让我自己执行MultiSelectList preference并覆盖像这样的prepareDialogBuilder方法
So now my questions are: is this normal/intended behavior or is this a bug? And how can I work around this? I already tried making my own implementation of MultiSelectListPreference and override the onPrepareDialogBuilder method like this
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
{
super.onPrepareDialogBuilder(builder);
Log.i("mmslp", Arrays.deepToString(PreferenceManager.getDefaultSharedPreferences(getContext()).getStringSet(getKey(), new HashSet<String>()).toArray()));
setValues(PreferenceManager.getDefaultSharedPreferences(getContext()).getStringSet(getKey(), new HashSet<String>()));
}
但值显然已经消灭了这一点。
but the values are apparently wiped already at this point.
推荐答案
我花了一些时间昨天就这个问题和我现在相信,它的目的不是这样的,但实际上打破 - 有人迷惑参考指针类型和价值。 ; - )
I spent some time yesterday on this and am now convinced that it is not intended like that, but actually broken - someone confused reference by pointer and by value. ;-)
好像(自4.1)虽然被定格在了Android的更新的版本: <一href="https://$c$c.google.com/p/android/issues/detail?id=22807">https://$c$c.google.com/p/android/issues/detail?id=22807
Seems to be fixed in the more current versions of Android (since 4.1) though: https://code.google.com/p/android/issues/detail?id=22807
我解决了,现在为Android的previous版本的方式是覆盖在我的执行MultiSelectList preference setValues方法将和公正的值复制到一个新的对象:
The way I solved it now for the previous versions of Android is to override the setValues method in my implementation of MultiSelectListPreference and just copy the values into a new object:
@Override
public void setValues( Set<String> values ) {
//Workaround for https://code.google.com/p/android/issues/detail?id=22807
final Set<String> newValues = new HashSet<String>();
newValues.addAll( values );
super.setValues( newValues );
}
这篇关于MultiSelectList preference不存储价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!