Android的可搜索ListPreference

时间:2018-08-27 20:28:55

标签: android android-preferences preferenceactivity listpreference preferencefragment

我的应用程序中有一个首选项屏幕,用户必须从列表中选择一个值:

<PreferenceCategory
        android:title="@string/entry_preferences">

    <ListPreference
        android:key="list_preference_access"
        android:title="@string/title_list_preference_access"
        android:summary="@string/summary_list_preference_access"
        android:dialogTitle="@string/title_list_preference_access" />

</PreferenceCategory>

然后在PreferenceFragment中:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        ListPreference listPreferenceAccess = (ListPreference) findPreference("list_preference_access");

        //Populates with a List
        if (listPreferenceAccess != null) {

            ArrayList<EventAccess> eaList = Statics.getEventAccessList();

            CharSequence entries[] = new String[eaList.size()];
            CharSequence entryValues[] = new String[eaList.size()];

            int i = 0;
            for (EventAccess ea : eaList) {
                entries[i] = ea.getName();
                entryValues[i] = Long.toString(ea.getId());
                i++;
            }
            listPreferenceAccess.setEntries(entries);
            listPreferenceAccess.setEntryValues(entryValues);
        }
    }

就像一个咒语一样工作,但是“ EventAccess” ArrayList具有+/- 150的值,并且在搜索项目时会遇到麻烦。我希望ListPreference中的DialogPreference是可搜索的。有可能吗?

0 个答案:

没有答案