简体   繁体   English

使用ArrayList创建MultiSelectedListPreference

[英]Create MultiSelectedListPreference using ArrayList

I'm trying to create a PreferenceActicity . 我正在尝试创建一个PreferenceActicity I need that on of my preferences will be of type MultiSelectedListPreference . 我需要我的首选项类型为MultiSelectedListPreference

I found this code on the internet: 我在互联网上找到了以下代码:

<MultiSelectListPreference
    android:dialogTitle="@string/mode_repeat"
    android:key="mode_repeat"
    android:summary=""        
    android:title="@string/mode_repeat"
    android:entries="@array/weekdays"
    android:entryValues="@array/weekdays_values"
    android:defaultValue="@array/empty_array"
    />

The problem is I'm getting the entries and entryValues in runtime. 问题是我在运行时获取entry和entryValues。 I'm building the ArrayList while my app is running, the question is how can I set my ArrayList as the entries and as the entryValues ? 我在运行应用程序时正在构建ArrayList ,问题是如何将ArrayList设置为条目和entryValues

Do I need to create an empty xml file, which I will re-write during the building of my list? 我是否需要创建一个空的xml文件,该文件将在构建列表时重新编写?

You wouldn't be able to change the xml in runtime. 您将无法在运行时更改xml。 The solution for your problem is to use the methods setEntries() and setEntryValues() from the MultiSelectListPreference class. 解决问题的方法是使用MultiSelectListPreference类中的setEntries()setEntryValues()方法。

Here's a basic code snippet: 这是一个基本的代码片段:

MultiSelectListPreference repeatModePreference = (MultiSelectListPreference) findPreference(Constants. mode_repeat);
repeatModePreference.setEntries(yourEntries); // This is human-readable strings
repeatModePreference.setEntryValues(yourEntryvalues) // The value corresponding to the human-readable string

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM