I have a PreferenceActivity with a bunch of (Sub)PreferenceScreens. Each such (Sub)PreferenceScreen represents an account and has the account-username as its title.
PreferenceScreen root = mgr.createPreferenceScreen(this);
for (MyAccountClass account : myAccounts) {
final PreferenceScreen accScreen = mgr.createPreferenceScreen(this);
accScreen.setTitle(account.getUsername());
// add Preferences to the accScreen
// (for instance a "change username"-preference)
...
root.add(accScreen);
}
As the user enters sub-PreferenceScreen, and edits the account user-name, I want the outer PreferenceScreen to update it's PreferenceScreen-title for the account in question.
I've tried to add...
usernamePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
accScreen.setTitle(newValue.toString());
return true;
}
});
...but the accScreen.setTitle does not seem to take effect on the outer PreferenceScreen. I've note that calling onContentChanged();
actually makes it work, but I realize that this is probably not the preferred way of doing it.
I suspect I should call postInvalidate()
on some view somewhere, but I really can't figure out on what view and when to do it.
PreferenceScreen android:summary update ! may be experiening the same problem as me.
Any help appreciated.
I found a solution to this. I have a hierarchy like this, each of these is a PreferenceScreen:
main settings
-> users list
-> user1 settings
-> user2 settings
...
In the users list, title of the sub-screen is dependent on the user settings. Now when I create the user list, I store the list adapter to a variable in my PreferenceActivity.
PreferenceScreen usersListScreen = ...
userScreenListAdapter = (BaseAdapter)usersListScreen.getRootAdapter();
Now when userX-settings are edited, I set the titles in the usersListScreen and after that call:
userScreenListAdapter.notifyDataSetChanged();
which updates the list UI and the changes are visible.
notifyDataSetChanged() is right solution. But I want to add recursive iterator for all PreferenceScreens, as far as I got a problem to find real parent of a Preference. For complicated structure of Preferences I recommend this killer-code:
private void updateAll_PrefereneScreens(PreferenceGroup group) {
if (group instanceof PreferenceScreen) {
BaseAdapter adapter = (BaseAdapter) ((PreferenceScreen) group).getRootAdapter();
adapter.notifyDataSetChanged();
}
for (int i=0; i<group.getPreferenceCount(); i++) {
Preference pref = group.getPreference(i);
if (pref instanceof PreferenceGroup) {
updateAll_PrefereneScreens((PreferenceGroup) pref);
}
}
}
I call it after every setSummary() to ensure it works properly:
findPreference("KEY").setSummary(str);
updateAll_PrefereneScreens(getPreferenceScreen());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With