如何在onPreferenceClick触发时动态填充ListPreference?
我有一个首选项活动,其语言
为ListPreference
,它显示可用的语言列表。我可以在调用 onCreate 时填充列表,但我想在用户单击它时填充列表。
这是java代码
:
public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
addPreferencesFromResource(R.xml.settings);
} catch (Exception e) {
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
if((preference instanceof ListPreference) && (preference.getKey().equals("language"))){
ListPreference lp = (ListPreference)preference;
CharSequence[] entries = { "English", "French" };
CharSequence[] entryValues = {"1" , "2"};
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
return false;
}
}
这是settings.xml
(首选项):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">
<CheckBoxPreference android:key="enabled" android:title="Application Status" android:summary="Enable or disable the application" />
<ListPreference
android:key="language"
android:title="Language"
android:dialogTitle="Application language"
android:summary="Select the Application language"
/>
</PreferenceCategory>
</PreferenceScreen>
我搜索但没有找到结果!每次我单击该列表时都会发生异常。
I have a preference activity that has a language
as ListPreference
which displays the available language list. I can fill the list when onCreate is called, but I want to fill the list when the user clicks on it.
this is the java code
:
public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
addPreferencesFromResource(R.xml.settings);
} catch (Exception e) {
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
if((preference instanceof ListPreference) && (preference.getKey().equals("language"))){
ListPreference lp = (ListPreference)preference;
CharSequence[] entries = { "English", "French" };
CharSequence[] entryValues = {"1" , "2"};
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
return false;
}
}
and this is the settings.xml
(preference) :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">
<CheckBoxPreference android:key="enabled" android:title="Application Status" android:summary="Enable or disable the application" />
<ListPreference
android:key="language"
android:title="Language"
android:dialogTitle="Application language"
android:summary="Select the Application language"
/>
</PreferenceCategory>
</PreferenceScreen>
I searched but found no result! An exception occurs every time I click on that list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您收到异常是因为您的 ListPreference 对象未初始化 - 您需要在 XML 中设置
entries
和entryValues
属性,或者在onCreate() 中以编程方式执行此操作
或在onCreatePreferences()
中(例如,当使用androidx.preference.PreferenceFragmentCompat
时)。如果您需要在初始化初始 ListPreference 对象后动态更改列表中的项目,则需要在 ListPreference 对象上设置
OnPreferenceClickListener
。使用您在 XML 中指定的键来获取首选项的句柄。由于填充
entries
和entryValues
数组的代码必须在onCreate()
和onPreferenceClick
中运行>,将其提取到单独的方法 -setListPreferenceData()
以避免重复是有意义的。这里是来自 google DeskClock 应用程序:
You are getting the exception because your ListPreference object is not initialized - you either need to set
entries
andentryValues
attributes in your XML or do it programatically inonCreate()
or inonCreatePreferences()
(e.g. when usingandroidx.preference.PreferenceFragmentCompat
).If if you need to change the items in the list dynamically after the initial ListPreference object has been initialized then a
OnPreferenceClickListener
needs to be set on the ListPreference object. Use the key you have specified in the XML to get a handle to the preference.Since the code to populate the
entries
andentryValues
arrays will have to be run both inonCreate()
and inonPreferenceClick
, it makes sense to extract it to a separate method -setListPreferenceData()
in order to avoid duplication.Here is another example from google's DeskClock app:
使用PreferenceFragment & JAVA设置键而不是
PreferenceActivity & XML,如 https://stackoverflow.com/a/13828912/1815624< 所示/a>,这个答案基于:如果您想要的是能够在初始化初始 ListPreference 对象后动态更改列表中的项目,那么您将需要附加
OnPreferenceClickListener
直接指向 ListPreference 对象。使用您在 JAVA 源代码中指定的密钥(如CUSTOM_LIST
)来获取首选项的句柄。由于填充
entries
和entryValues
数组的代码必须在onCreate()
和onPreferenceClick
中运行,将其提取到单独的方法 -setListPreferenceData()
以避免重复是有意义的。XML 布局:
Using PreferenceFragment & JAVA set key rather than
PreferenceActivity & XMLas shown in https://stackoverflow.com/a/13828912/1815624, which this answer is based on:If what you want is to be able to change the items in the list dynamically after the initial ListPreference object has been initialized then you will need to attach the
OnPreferenceClickListener
directly to the ListPreference object. Use the key you have specified in the JAVA source (asCUSTOM_LIST
) to get a handle to the preference.Since the code to populate the
entries
andentryValues
arrays will have to run both inonCreate()
and inonPreferenceClick
, it makes sense to extract it to a separate method -setListPreferenceData()
in order to avoid duplication.XML layout:
我解决了扩展 ListPreference 的问题。很简单。
然后我只是在 XML 中将名称更改为我的类。
然后我只是在 onCreate 中执行了此操作。
马上就开始工作了。事实上,我没想到这件事会这么容易完成。
I solved the problem my extending the ListPreference. It was very simple.
And then I just changed the name to my class in the XML.
And then I simply did this at the onCreate.
Worked right away. Actually, I never expected it would be done so easily.
这就是我必须做的才能让我的工作成功
This is what I had to do to make mine work