我正在尝试设置ListPreference项目的defaultValue。
以下是我的preference.xml文件的示例:
<ListPreference android:key="notification_delay"
android:title="@string/settings_push_delay"
android:entries="@array/settings_push_delay_human_value"
android:entryValues="@array/settings_push_delay_phone_value"
android:defaultValue="????">
</ListPreference>
这两个数组:
<string-array name="settings_push_delay_human_value">
<item>every 5 minutes</item>
<item>every 10 minutes</item>
<item>every 15 minutes</item>
</string-array>
<string-array
name="settings_push_delay_phone_value">
<item>300</item>
<item>600</item>
<item>900</item>
</string-array>
当我进入preference活动时,ListPreference中没有任何项被选中。我尝试在"android:defaultValue“中设置一个类似于1的int值,以选择"10分钟”,但它不起作用。
<ListPreference android:key="notification_delay"
android:title="@string/settings_push_delay"
android:entries="@array/settings_push_delay_human_value"
android:entryValues="@array/settings_push_delay_phone_value"
android:defaultValue="1">
</ListPreference>
有什么想法吗?
发布于 2011-05-17 23:37:49
碰巧在同样的情况下。指定一致的默认值。但是图形化地没有被选中。我清除了申请数据。然后它就像预期的那样工作了。因此,在开发时添加新的XxxPreference项时,清除可能会很有用。
发布于 2014-01-30 06:24:19
除了Sven的答案之外,您还必须在启动活动中调用setDefaultValues()方法。这将设置所有默认值一次。
public class MainActivity extends Activity {
protected void onCreate(final Bundle savedInstanceState) {
// Set all default values once for this application
// This must be done in the 'Main' first activity
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
...
}
}
发布于 2014-01-09 11:57:47
如果它是列表中的有效值,则重新安装应用程序。看起来不错。
https://stackoverflow.com/questions/3770100
复制相似问题