How to fill ListPreference dynamically when onPreferenceClick is triggered?

前端 未结 3 1833
庸人自扰
庸人自扰 2020-12-13 09:55

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

3条回答
  •  春和景丽
    2020-12-13 10:17

    Using PreferenceFragment & JAVA set key rather than PreferenceActivity & XML as 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 (as CUSTOM_LIST) to get a handle to the preference.

    Since the code to populate the entries and entryValues arrays will have to run both in onCreate() and in onPreferenceClick, it makes sense to extract it to a separate method - setListPreferenceData() in order to avoid duplication.

    /**
     * This fragment shows data and sync preferences only. It is used when the
     * activity is showing a two-pane settings UI.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class myCustomPreferenceFragment extends PreferenceFragment {
    
        final private String CUSTOM_LIST= "custom_list";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_custom_frag);
    
            PreferenceCategory targetCategory = (PreferenceCategory) findPreference("CUSTOM_FRAG");
    
            final ListPreference lp = setListPreferenceData((ListPreference) findPreference(CUSTOM_LIST), getActivity());
    
            lp.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
    
                    setListPreferenceData(lp, getActivity());
                    return false;
                }
            });
            setHasOptionsMenu(true);
            targetCategory.addPreference(lp);
    
            bindPreferenceSummaryToValue(targetCategory);
            bindPreferenceSummaryToValue(lp);
        }
    
        protected ListPreference setListPreferenceData(ListPreference lp, Activity mActivity) {
            CharSequence[] entries = { "One", "Two", "Three" };
            CharSequence[] entryValues = { "1", "2", "3" };
            if(lp == null)
                lp = new ListPreference(mActivity);
            lp.setEntries(entries);
            lp.setDefaultValue("1");
            lp.setEntryValues(entryValues);
            lp.setTitle("Number Of blahs");
            lp.setSummary(lp.getEntry());
            lp.setDialogTitle("Number of Blah objects");
            lp.setKey(CUSTOM_LIST);
            return lp;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == android.R.id.home) {
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    

    XML layout:

    
    
        
    
        
    
    

提交回复
热议问题