Search code examples
javaandroidarraylistpreferenceactivityandroid-multiselectlistpreference

Create MultiSelectedListPreference using ArrayList


I'm trying to create a PreferenceActicity. I need that on of my preferences will be of type 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. 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?

Do I need to create an empty xml file, which I will re-write during the building of my list?


Solution

  • You wouldn't be able to change the xml in runtime. The solution for your problem is to use the methods setEntries()and setEntryValues()from the MultiSelectListPreference class.

    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.