Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceActivity update summary

Tags:

android

I have a PreferenceActivity with a 2 level tree of PreferenceScreens, something like:

<PreferenceScreen>
  <PreferenceScreen android:key="A">
     <ListPreference/>
     <EditTextPreference/>
  </PreferenceScreen>
  <PreferenceScreen android:key="B">
     <ListPreference/>
     <EditTextPreference/>
  </PreferenceScreen>
  ...
</PreferenceScreen>

Each of the lower level preference screens, e.g., A and B, is collecting two related pieces of data. I want the summary for those parent items to be a combination of the current values of the two sub preferences.

I tried adding onPreferenceChangeListener's on the leaf preferences and updating the summary from there, but it doesn't seem to take. The preferences are all created programmatically within the activity, so I'm doing something like this in onCreate:

   leafListPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
       @Override
       public boolean onPreferenceChange(Preference preference, Object newValue) {
              // do some work
              prefScreenA.setSummary( /* get new summary based on newValue */);
              return true;
       }
  });

I then tried to find a location where I can be informed that I have returned to the top level preferences screen from a subpage so that I can update at that point. However, I am confused as to how the lower level screens are being displayed. It looks like they are actually dialogs, not full Activities since onPause/onResume is not called when moving between them. Is there some method somewhere that I'm missing that will be called when returning to the top level page?

I also tried adding a sharedPreferenceChangeListener, as described here, but that never seems be called.

Anyone have any hints on what I'm missing here, or any easier approach I'm missing?

like image 454
Cheryl Simon Avatar asked Jan 11 '11 01:01

Cheryl Simon


2 Answers

I had the same problem and it seems that calling Activity.onContentChanged() after the summary is changed corrected my problem.

@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
  // Update summary value
  EditTextPreference pref = (EditTextPreference)findPreference(key);
  pref.setSummary(pref.getText());
  this.onContentChanged();
}
like image 164
jmbouffard Avatar answered Oct 06 '22 22:10

jmbouffard


After a truly asinine amount of trial and error, I finally found the answer in another SO answer. Here's the magic line:

((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();

Put that in your preference listener, then you're good to go.

like image 40
shawkinaw Avatar answered Oct 06 '22 22:10

shawkinaw