简体   繁体   English

跨活动在SharedPreference中存储价值

[英]Store Value in SharedPreference across Activities

I am new in Android, and got a task to develop a small project to submit in my college, i have tried my best but now i need some help 我是Android的新手,有个任务要开发一个小项目并提交我的大学,我已经尽力了,但是现在我需要一些帮助

Let me tell you first, what i am trying to do ? 我先告诉你, what i am trying to do ?

I have two Activities, MainActivity and AccountActivity 我有两个活动, MainActivityAccountActivity

In MainActivity i am using button to switch to AccountActivity MainActivity我使用button 切换AccountActivity

In AccountActivity i am trying to store strBalance value in SharedPreference but whenever i do click on back button and then again come to AccountActivity always getting "1000" not that value which i have stored in SharedPreference . AccountActivity我试图将strBalance值存储在SharedPreference中,但是每当我单击back按钮,然后再次进入AccountActivity总会得到"1000"而不是我在SharedPreference存储的值。

Here is the complete code of AccountActivity.java:- 这是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();
                }
            }
        });
    }

}

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

This should work fine: 这应该工作正常:

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

Instead of 代替

strBalance = 1000;

Do this, 做这个,

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

Remove initialisation and do this in your onCreate and then use the new variable as you strBalance. 删除初始化,并在onCreate中执行此操作,然后在strBalance时使用新变量。

You did not retrieve data from SharedPreferences 您没有从SharedPreferences检索数据

Write your code in onResume() like this: 像这样在onResume()编写代码:

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

To know more about SharedPreferences check this link . 要了解有关SharedPreferences更多信息,请检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM