从另一个活动获取和设置 listPreference 不起作用

发布于 2024-11-10 08:44:07 字数 1076 浏览 5 评论 0原文

我试图从不同的活动获取并设置 listPreference 值,但它不起作用。

当我从主要活动中读取和写入它时,它只保留我写的内容,因此我假设当我退出活动时我没有正确定位 listPreference ,因为它在我的偏好活动中工作没有问题。

我在开发者网站上看到了一些关于 CharSequence 和 getValue 和 getEntryValues 的参考,但我也没有运气让它们正常工作。

这是我的代码,用于单击按钮并设置列表首选项值,然后启动切换活动的意图:

主活动,尝试将列表首选项的值设置为第一个索引值;

        SharedPreferences settings = getSharedPreferences("PreferenceXML", 
                MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();

            editor.putString("ListPreferenceInXML", "1");
            editor.commit();

            String levelCheck = settings.getString("ListPreferenceInXML","1");

在我的下一个活动中,我调用“读取启动时的值”来查看哪个 listPreference 处于活动状态,它始终是我从上面列出的电源活动中写入的数字。问题是,当我转到实际的首选项活动时,当我在 ListPreference 上更改它并从那里启动相同的活动(它仍然读取我从主活动按钮设置的值)时,它不匹配或更新,

代码如下:尝试读取 ListPreference 的活动:

    SharedPreferences settings = getSharedPreferences("PreferenceXML", 
                MODE_PRIVATE);

    Toast.makeText(this, settings.getString("ListPreferenceInXML","1"), 1000).show();

I'm trying to get and set a listPreference value from different activities and it's not working.

When I read and write it from my main activity, it only keeps whatever I write, so I'm assuming that I'm not targeting the listPreference correctly when I'm out of the activity because it's working inside my preference activity no problem.

I've seen some references on the developer website to CharSequence with getValue and getEntryValues but I haven't had luck getting them to work correctly either.

Here is my code for clicking a button and setting the listpreference value then it launches an intent to switch activities:

Main Activity, attempting to set the value of the listpreference to the first index value;

        SharedPreferences settings = getSharedPreferences("PreferenceXML", 
                MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();

            editor.putString("ListPreferenceInXML", "1");
            editor.commit();

            String levelCheck = settings.getString("ListPreferenceInXML","1");

In my next activity I call read the value on launch to see which listPreference is active and it is always the number I write from the mains activity listed above. The problem is when I goto the actual Preference activity and it doesn't match or update when I change it on the ListPreference and launch the same activity from there (it still reads the value I set from the Main activity button)

code as follows for activity trying to read ListPreference:

    SharedPreferences settings = getSharedPreferences("PreferenceXML", 
                MODE_PRIVATE);

    Toast.makeText(this, settings.getString("ListPreferenceInXML","1"), 1000).show();

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

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

发布评论

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

评论(1

橙味迷妹 2024-11-17 08:44:07

所以我终于弄清楚了,问题出在我调用首选项的方式上。在这两种情况下,不要像这样调用首选项;

   SharedPreferences settings = getSharedPreferences("PreferenceXML", 
            MODE_PRIVATE);

像这样调用它们:

   SharedPreferences settings = 
        PreferenceManager.getDefaultSharedPreferences(getBaseContext());

我不确定调用首选项的第一种方法是否缺少一个步骤,但第二种方法效果很好。

So I finally figured it out, the problem was with the way I was calling the preferences. Instead of calling the preferences like this, in both cases;

   SharedPreferences settings = getSharedPreferences("PreferenceXML", 
            MODE_PRIVATE);

Call them like this:

   SharedPreferences settings = 
        PreferenceManager.getDefaultSharedPreferences(getBaseContext());

I'm not sure if there is a step missing out of the first way of calling the preferences but this 2nd way worked like a champ.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文