Android 属性在编辑和提交后未更新

发布于 2024-12-05 00:44:28 字数 650 浏览 3 评论 0原文

在我的 PreferenceActivity 的 onCreate 方法中,我设置了一些如下所示的属性:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);      
SharedPreferences.Editor editor = preferences.edit();

editor.putBoolean("pref_mykey", true);

editor.commit();

它似乎可以工作,但视图没有更新,因此当我打开属性时,onCreate 会运行并更改值。然后,我将在屏幕上看到“旧”值,直到我关闭并重新打开首选项屏幕,然后我确实会看到新属性。

我在提交后已经尝试过这个:

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

但没有任何效果。我仍然看到“旧”首选项,直到我关闭并重新打开 PreferenceActivity 。

我做错了什么?在 onCreate 方法中设置值后如何刷新 PreferenceActivity?

非常感谢您的帮助。

in the onCreate method of my PreferenceActivity i set some Properties like this:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);      
SharedPreferences.Editor editor = preferences.edit();

editor.putBoolean("pref_mykey", true);

editor.commit();

It seems to work but the View is not updating so when i open the properties the onCreate runs and changes the values. I will then see the "old" values on the Screen until i close and reopen the prefs screen, then i do see the new properties.

I already tried this after the commit:

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

But without any effect. I still see the "old" Prefs until i close and reopen the PreferenceActivity .

What am i doing wrong? How can i refresh the PreferenceActivity after setting values in the onCreate method?

Thanks a lot for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-12-12 00:44:28

这困扰了我一整天,但终于找到了解决方案。

((ListPreference)getPreferenceScreen().findPreference("key_of_preference_in_xml_file")).setValue(code);

你的例子是一个布尔值,所以它必须根据它的偏好进行改变。
IE:
((CheckBoxPreference)getPreferenceScreen().findPreference("key_of_preference_in_xml_file")).setChecked(true);

This was buggin me all day but finally found a solution.

((ListPreference)getPreferenceScreen().findPreference("key_of_preference_in_xml_file")).setValue(code);

Your example is with a boolean so it'll have to change based on what kind of preference it is.
IE:
((CheckBoxPreference)getPreferenceScreen().findPreference("key_of_preference_in_xml_file")).setChecked(true);

我不咬妳我踢妳 2024-12-12 00:44:28

如果您使用 PreferenceFragment,则需要在活动中注入 PreferenceScreen。

public class SettingsActivity extends Activity implements ScanFragment.OnFragmentInteractionListener {

    private PreferenceScreen prefScreen;

    public void setPreferenceScreen(PreferenceScreen prefScreen){
        this.prefScreen = prefScreen;
    }

    @Override
    public void onFragmentInteraction(String scanContent) {
        // commiting to editor retrieved from preference manager would not refresh view
        EditTextPreference pref = (EditTextPreference)this.prefScreen.findPreference(getString(R.string.preference_url_key));
        pref.setText(scanContent);
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
            ((SettingsActivity)this.getActivity()).setPreferenceScreen(this.getPreferenceScreen());
        }
    }
}

If you use a PreferenceFragment than you need to inject the PreferenceScreen in the activity.

public class SettingsActivity extends Activity implements ScanFragment.OnFragmentInteractionListener {

    private PreferenceScreen prefScreen;

    public void setPreferenceScreen(PreferenceScreen prefScreen){
        this.prefScreen = prefScreen;
    }

    @Override
    public void onFragmentInteraction(String scanContent) {
        // commiting to editor retrieved from preference manager would not refresh view
        EditTextPreference pref = (EditTextPreference)this.prefScreen.findPreference(getString(R.string.preference_url_key));
        pref.setText(scanContent);
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
            ((SettingsActivity)this.getActivity()).setPreferenceScreen(this.getPreferenceScreen());
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文