代码中的MultiSelectListPreference

时间:2012-12-09 13:12:10

标签: java android sharedpreferences preferenceactivity

我想知道如何在代码中使用 MultiSelectListPreference 首选项。

以下代码来自API示例文件PreferencesFromCode.java,如果有人可以为MultiSelectListPreference提供类似的示例,那么您可以将我的一天:)

// List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
listPref.setEntryValues(R.array.entryvalues_list_preference);
listPref.setDialogTitle(R.string.dialog_title_list_preference);
listPref.setKey("list_preference");
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref);

1 个答案:

答案 0 :(得分:3)

我在网上找不到任何例子,但我把它放在一起就可以了。

修改:此解决方案仅适用于ICS +。 Honeycomb完全忽略.setValues(),传递给侦听器的HashSet包含不同的值。这是一个已知的错误,但我希望这可以帮助那些想要在Android v4 +中实现的人

    MultiSelectListPreference listPreference = new MultiSelectListPreference(context);
    listPreference.setTitle(R.string.configure_category_title);
    listPreference.setDialogTitle(R.string.configure_category_title);
    listPreference.setSummary(R.string.configure_category_summary);
    listPreference.setEntries(R.array.configure_category_array);
    listPreference.setEntryValues(new CharSequence[]{
            ProcessList.PREF_SERVICES + mAppWidgetId,
            ProcessList.PREF_INACTIVE + mAppWidgetId,
            ProcessList.PREF_INTERNAL + mAppWidgetId
    });

    //Create a Set<String> with list items that should be selected
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean showServices = sharedPref.getBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, true);
    boolean showInactive = sharedPref.getBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, true);
    boolean showInternal = sharedPref.getBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, true);

    String[] strings = new String[3];
    int cnt = 0;
    if (showServices)
        strings[cnt++] = ProcessList.PREF_SERVICES + mAppWidgetId;
    if (showInactive)
        strings[cnt++] = ProcessList.PREF_INACTIVE + mAppWidgetId;
    if (showInternal)
        strings[cnt] = ProcessList.PREF_INTERNAL + mAppWidgetId;

    Set<String> mySet = new HashSet<String>();
    Collections.addAll(mySet, strings);

    //Add the set
    listPreference.setValues(mySet);

    //Listen for changes, I'm not sure if this is how it's meant to work, but it does :/
    listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object o) {

            HashSet hashSet = (HashSet) o;
            Iterator stringIterator = hashSet.iterator();
            boolean[] states = {false, false, false};
            String prefString;

            while (stringIterator.hasNext()) {

                prefString = (String) stringIterator.next();

                if (prefString == null)
                    continue;

                if (prefString.compareTo(ProcessList.PREF_SERVICES + mAppWidgetId) == 0)
                    states[0] = true;
                else if (prefString.compareTo(ProcessList.PREF_INACTIVE + mAppWidgetId) == 0)
                    states[1] = true;
                else if (prefString.compareTo(ProcessList.PREF_INTERNAL + mAppWidgetId) == 0)
                    states[2] = true;

            }

            PreferenceManager
                    .getDefaultSharedPreferences(getActivity())
                    .edit()
                    .putBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, states[0])
                    .putBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, states[1])
                    .putBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, states[2])
                    .commit();

            return true;
        }
    });

    preferenceCategory.addPreference(listPreference);