JRL
JRL

Reputation: 78023

How to access all the children of a PreferenceScreen?

I'm trying to programmatically uncheck all the CheckBoxPreference children of a PreferenceScreen in my app. How can I do that?

Upvotes: 0

Views: 1180

Answers (2)

JRL
JRL

Reputation: 78023

Found a simpler way - sufficient for my use case - which is to clear the preferences. This can be done by a static method provided a context is passed to it:

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();

Upvotes: 2

Cristian
Cristian

Reputation: 200150

Cannot test it right now... but I'd try something like this:

final ListAdapter adapter = getPreferenceScreen().getRootAdapter();
for (int idx = 0; idx < adapter.getCount(); idx++) {
    Object object = adapter.getItem(idx);
    if(object instanceof CheckBoxPreference){
        ((CheckBoxPreference)object).setChecked(false);
    }
}

Upvotes: 3

Related Questions