SharePrefs不适用于Android旋转器 - 行为非常不稳定

发布于 2025-01-28 17:13:51 字数 1528 浏览 2 评论 0原文

我在对话框的内部有一个旋转器,您可以用它为我的应用程序选择货币。当您选择货币时,它将连接到货币站点的API,并使用JSON显示分析的信息,以在我的应用程序的MainAttivity中显示所选货币的信息。

我已经使用了共享的首选项来保存所选旋转器选择的索引NO,并且在启动方法开始时具有一些编码,以检索活动启动时已保存的旋转器索引NO,然后将应用与之相关的货币通过再次使用该网站的API和JSON的索引。

共享首选项文件似乎在我进入该索引进行检查时使用所选索引NO更新,但是在开始活动后,它将显示正确的货币,然后很快将其更换为其他一个。从共享prefs检索和应用正确的索引否的行为似乎非常不稳定。是否有人可以看一下并建议我可能出错的地方?

使用旋转器时的代码的一个示例是:

  if (newIndex == 1) {

                        // save inputted spinner position to sharedpreferences
                        int userChoice = powerSpinnerView.getSelectedIndex();

                        SharedPreferences sharedPref =                                                   
                        getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPref.edit();

                        editor.clear();

                        editor.putInt("UserChoice", userChoice);
                        editor.apply();

                        Toast.makeText(getApplicationContext(),"CURRENCY CHANGED TO GBP",Toast.LENGTH_SHORT).show();

用于启动时应用共享pref的代码:

     // Retrieve spinner position from sharedpreferences
    SharedPreferences sharedPref = getSharedPreferences("MySharedPref", 
    Context.MODE_PRIVATE);

    int spinnerValue =  sharedPref.getInt("UserChoice", -1);

    if (spinnerValue != 1){
    // set the value of the spinner

    Toast.makeText(getApplicationContext(),"GBP 
    SELECTED",Toast.LENGTH_SHORT).show();

I have a spinner inside of a dialog box, which you can use to select a currency for my app. When you select a currency, it connects to an api from a currency site and displays the parsed info using Json to display the chosen currency's info in my app's mainactivity.

I have used shared preferences to save the index no of the selected spinner choice and have some coding at the beginning of the oncreate method to retrieve the saved spinner index no when the activity is started back up, which will then also apply the currency associated with the index by using the site's api and json again.

The shared preferences file seems to update with the selected index no when I go into it to check, but upon starting the activity it will show the right currency, before very quickly changing to one of the others. The behaviour in retrieving and applying the right index no from shared prefs seems very erratic. Is anybody able to take a look and advise where I might be going wrong please?

An example of the code for when the spinner is used is:

  if (newIndex == 1) {

                        // save inputted spinner position to sharedpreferences
                        int userChoice = powerSpinnerView.getSelectedIndex();

                        SharedPreferences sharedPref =                                                   
                        getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPref.edit();

                        editor.clear();

                        editor.putInt("UserChoice", userChoice);
                        editor.apply();

                        Toast.makeText(getApplicationContext(),"CURRENCY CHANGED TO GBP",Toast.LENGTH_SHORT).show();

The code for applying shared prefs upon start up:

     // Retrieve spinner position from sharedpreferences
    SharedPreferences sharedPref = getSharedPreferences("MySharedPref", 
    Context.MODE_PRIVATE);

    int spinnerValue =  sharedPref.getInt("UserChoice", -1);

    if (spinnerValue != 1){
    // set the value of the spinner

    Toast.makeText(getApplicationContext(),"GBP 
    SELECTED",Toast.LENGTH_SHORT).show();

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文