开发者

setSummary(txt) in onSharedPreferenceChanged issue

开发者 https://www.devze.com 2023-02-21 05:33 出处:网络
I have a PreferenceScreen defined in an XML-file like this: <PreferenceCategory android:title=\"Choose Days\">

I have a PreferenceScreen defined in an XML-file like this:

<PreferenceCategory android:title="Choose Days">
    <PreferenceScreen android:title="Days of Week" android:key="daysOfWeek">
        <CheckBoxPreference android:titl开发者_如何学编程e="Mondays" android:key="chkMonday"></CheckBoxPreference>
        <CheckBoxPreference android:title="Tuesdays" android:key="chkTuesday"></CheckBoxPreference>
        <CheckBoxPreference android:title="Wednesdays" android:key="chkWednesday"></CheckBoxPreference>
        <CheckBoxPreference android:title="Thursdays" android:key="chkThursday"></CheckBoxPreference>
        <CheckBoxPreference android:title="Fridays" android:key="chkFriday"></CheckBoxPreference>
        <CheckBoxPreference android:title="Saturdays" android:key="chkSaturday"></CheckBoxPreference>
        <CheckBoxPreference android:title="Sundays" android:key="chkSunday"></CheckBoxPreference>
    </PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Other Settings">
    <CheckBoxPreference android:title="Enable" android:defaultValue="true" android:key="enable"></CheckBoxPreference>
</PreferenceCategory>

When I click on the PreferenceScreen:daysOfWeek the checkboxes appear, og when I check or uncheck a box, the onSharedPreferenceChanged is triggered.

This is because of this:

public class Muteny extends PreferenceActivity implements OnSharedPreferenceChangeListener {

and register it in onResume and unregister it in onPause in my .java file.

A simplified onSharedPreferenceChanged looks like this:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    PreferenceScreen daysOfWeek = ((PreferenceScreen)findPreference("daysOfWeek"));
    daysOfWeek.setSummary("text for summary");
    Toast.makeText(this.getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
}

The problem is that the summary of "daysOfWeek" is never updated. If I then switch the CheckBoxPreference "enable" (which does nothing at the moment, but trigger the change), the summary is updated as I wanted it to do when switching the "day-boxes".

The message in Toast IS displayed though...

How do I update the summary of my PreferenceScreen when checking the "day boxes"?


This issue comes up frequently.

Here are a couple of other answers you can look at for possible solutions. I've never settled on a solution that makes me 100% happy though.

PreferenceScreen android:summary update !

Update existing Preference-item in a PreferenceActivity upon returning from a (sub)PreferenceScreen

Of all of the "solutions" I've seen the one that seems to work best is to call onContentChanged() after your setSummary(...) call. There is some debate as to whether this is safe to do.

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    if (key.startsWith("chk")) {            
        PreferenceScreen prefscreen = ((PreferenceScreen)findPreference("daysOfWeek"));
        prefscreen.setSummary(key);
        onContentChanged();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消