如何将新值添加到listpreference并保存? [英] How to add new value to listpreference and save it?
问题描述
我有一个从资源xml创建的列表首选项.我添加了创建对话框的首选项,用户可以在其中向listPreference添加值.
I have a list Preference that created from resource xml. I added preference that created dialog in which user can add value to listPreference.
使用这些方法,我添加了新的价值:
using those methods i added new value:
entries = getEntries();
entryValues = getEntryValues();
当用户将值添加到listpreference时,其显示.但是,当preferenceScreen重新创建新值时,它就会消失.
when user is adding values to listpreference, its displayed. But when preferenceScreen is recreating new value disappearing.
如何保存这些新值?
推荐答案
问题是,当您重新打开PreferenceScreen
时,它会从XML加载ListPreference
的值.您可以使用ListPreference
的setEntries()
和setEntryVaues()
方法更改此行为.当然,您需要以某种方式存储用户输入的所有值及其索引.您可以使用数据库或SharedPreferences
.希望这会有所帮助.
Problem is that when you're reopening your PreferenceScreen
, it loads the ListPreference
's values from XML. You can change this behavior using the setEntries()
and setEntryVaues()
methods of ListPreference
. Of course you need to somehow store all the values and their indexes that your users enter. You can use databases or SharedPreferences
for it. Hope this helps.
编辑
将ListPreference
的值保存到SharedPreferences
中:
preferences.edit().putString(listPreference.getKey(), listPreference.getValue());
这篇关于如何将新值添加到listpreference并保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!