Android CheckBoxPreference - un / check all settings

I have a PreferenceScreen containing only CheckBoxPreferences (categories for selection). I need to check / uncheck all the checkboxes. I have the following code that does the job, but there is one problem: the flags on the screen are not updated - I will need to call some invalid ones in the view or something like that.

Here is my real code:

  private void checkAll() { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = settings.edit(); @SuppressWarnings("unchecked") Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll(); for(String s : categories.keySet()) { editor.putBoolean(s, true); } editor.commit(); } private void uncheckAll() { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = settings.edit(); @SuppressWarnings("unchecked") Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll(); for(String s : categories.keySet()) { editor.putBoolean(s, false); } editor.commit(); this.restart(); } 

This code works fine, but I need to update the view to see the imediatelly result (not only after closing and restarting the settings activity).

Thanks everyone for any advice!

+6
source share
2 answers

This is because you are not actually capturing preference objects, but only actual values. Try this in your loops:

  boolean theValue = param; for(String s : categories.keySet()) { Preference pref = findPreference(s); if (pref instanceof CheckBoxPreference) { CheckBoxPreference check = (CheckBoxPreference)pref; check.setChecked(theValue); } } 

I think you can omit the editor call and commit, since setChecked () will do this for you.

Also check this one out .

+14
source

Now I have this working code, maybe this will help someone:

 import ... public class CategoriesActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.category_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.categories_check_all: this.checkAll(); return true; case R.id.categories_uncheck_all: this.uncheckAll(); return true; } return false; } private void checkAll() { this.setCheckState(true); } private void uncheckAll() { this.setCheckState(false); } private void setCheckState(Boolean state) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); @SuppressWarnings("unchecked") Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll(); for (String s : categories.keySet()) { Preference pref = findPreference(s); if (pref instanceof CheckBoxPreference) { CheckBoxPreference check = (CheckBoxPreference) pref; check.setChecked(state); } } } } 
0
source

Source: https://habr.com/ru/post/887153/


All Articles