如何在活动中的SharedPreferences中保存值并访问所有其他类

时间:2013-09-30 12:43:52

标签: android sharedpreferences

我希望将此活动中的totalBalance保存到SharedPreferances,并在另一个类中将其恢复。

所以我希望在同一个应用程序中的其他活动中显示totalbalance ... 如果可能的话,也可以在其他活动中编辑它......

请帮忙......谢谢

    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button click

            // Genrating random number Random number
            Random rn = new Random();
            randomNumber = (int) rn.nextInt(9) + 1;

            // changes textView1 equals to random number
            textView1.setText("Random Number is "
                    + Integer.toString(randomNumber));
            button1.setText("Play Again");

            // Matching random number to ArrayList
            if (positive_IDs.contains(randomNumber)) {
                // if matched then changes textView2 to Matched Number
                textView2.setText("Number: "
                        + Integer.toString(randomNumber) + " Matched");

                totalBalance = totalBalance + winingPrize;
                textView5.setText("Total Balance = Rs: "
                        + String.format("%.2f", totalBalance));



            }

        }

4 个答案:

答案 0 :(得分:4)

试试这个,

public  void saveValue(String lock, Context context) {
    Editor editor = context
            .getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit();
    editor.putString("Value", lock);
    editor.commit();
}

public  String getValue(Context context) {
    SharedPreferences savedvalue = context.getSharedPreferences(KEY,
            Activity.MODE_PRIVATE);
    return savedvalue.getString("Value", "");

}

答案 1 :(得分:4)

将价值保存为以下

 int Value;
    private void saveValues(){
    SharedPreferences readSP = getSharedPreferences("String", MODE_PRIVATE);
            SharedPreferences.Editor editor = readSP.edit();
            editor.putString("String", Value);
            editor.commit();
    }

在以下任何类中检索值

int value;


private void getSavedValue()
{
SharedPreferences settings = getSharedPreferences("String", MODE_PRIVATE);
value=settings.getString("String", "");
}

答案 2 :(得分:1)

使用以下代码从SharedPreference中检索您的值,将其写入您想要检索总余额值的其他类

 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(YourActivityName.this);

Editor edit1 = remembermepref.edit();
            edit1.putInt("totalbalance_key",totalBalance);
            edit1.commit();

并将总余额存储到您的活动中的ShardPreference使用中:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(YourActivityName.this);
int totalbalance = pref.getInt("totalbalance_key");

现在使用你想要的totalbalance方式。     最重要的是检查是否使用了相同的密钥进行恢复以及在SharedPreference中使用了该值

希望这会有所帮助..

答案 3 :(得分:1)

// try this
 SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
 SharedPreferences.Editor editor = sharedPreferences.edit();
 editor.putBoolean("balance", String.format("%.2f", totalBalance));
 editor.commit();

 SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
 String total = sharedPreferences.getString("balance");