【问题标题】:Store Value in SharedPreference across Activities跨活动在 SharedPreference 中存储价值
【发布时间】:2015-03-16 06:18:24
【问题描述】:

我是 Android 新手,我的任务是开发一个小项目以在我的大学提交,我已经尽了最大努力,但现在我需要一些帮助

我先告诉你,what i am trying to do ?

我有两个活动,MainActivityAccountActivity

MainActivity 中,我使用button 切换AccountActivity

AccountActivity 中,我试图将strBalance 值存储在SharedPreference 中,但每当我点击back 按钮然后再次来到AccountActivity 总是得到"1000" 而不是我存储在@ 中的那个值987654332@.

下面是AccountActivity.java的完整代码:-

public class AccountActivity extends Activity {

    EditText editAmount;
    int strAmount;
    Button btnPayment;
    TextView textBalance;
    int strBalance; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_account);      

        editAmount = (EditText) findViewById(R.id.editAmt);
        btnPayment = (Button) findViewById(R.id.btnPay);
        textBalance = (TextView) findViewById(R.id.textBalance);

        strBalance = 1000;

        textBalance.setText("Your current balance is: $ "+strBalance);      

        btnPayment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub              
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);  
                Editor editor = pref.edit();

                strAmount = Integer.parseInt(editAmount.getText().toString().trim()); // user input             

                if(strAmount>strBalance) {
                    Toast.makeText(AccountActivity.this, "You don't have enough balance", Toast.LENGTH_SHORT).show();
                }
                else 
                {
                    strBalance = strBalance - strAmount; // 1000 - 500 = 500
                    textBalance.setText("Your current balance is: $ "+strBalance);  
                    Log.v("strBalance", String.valueOf(strBalance)); // 500
                    editor.putInt("key_balance", strBalance);
                    editor.commit();
                }
            }
        });
    }

}

【问题讨论】:

  • 使用日志消息并检查它是否被正确保存。在保存之前打印 strBalance 的值并检查您是否获得了正确的值。
  • 请发布您的主要活动部分,您正在尝试检索已保存的数据。
  • 我没有尝试在 MainActivity 中检索数据,我将数据存储在 AccountActivity 中并希望在恢复时显示在同一个中,并且我已经检查了我是否根据我的要求在 strBalance 中获得了价值
  • Ohhoo...我找到了原因,我正在使用 strBalance = 1000;在 onCreate(..) 但我该如何处理这个@Kesh1234
  • 为什么要在 onCreate 中初始化?

标签: java android sharedpreferences


【解决方案1】:

这样做,

SharedPreferences sharedPreferences = getSharedPreferences("MyPref", MODE_PRIVATE);
       int any_variable = sharedPreferences.getInt("key_balance", 0);

删除初始化并在您的 onCreate 中执行此操作,然后在您的 strBalance 中使用新变量。

【讨论】:

  • 酷。感谢您接受答案。编码愉快。
【解决方案2】:

-检查首选项键名是否相同。

【讨论】:

    【解决方案3】:

    您没有从SharedPreferences检索数据

    onResume() 中写下您的代码,如下所示:

    @Override
    protected void onResume() {
        super.onResume();
        // SharedPreferences retrieval code
    }
    

    要了解更多关于SharedPreferences的信息,请查看link

    【讨论】:

      【解决方案4】:

      这应该可以正常工作:

      SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
      if (pref.getInt("key_balance", 1000) != null) {
          strBalance = pref.getInt("key_balance",1000);
      } else {
          strBalance = 1000;
      }
      

      而不是

      strBalance = 1000;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        • 2016-08-23
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 2014-01-17
        相关资源
        最近更新 更多