0

我想将此活动中的 totalBalance 保存到 SharedPreferances 并在另一个类中检索它。

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

请帮助...谢谢

    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

4 回答 4

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", "");
}
于 2013-09-30T12:49:49.290 回答
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", "");

}
于 2013-09-30T12:45:17.457 回答
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");

现在使用你想要的总平衡方式。最重要的是检查您是否使用相同的密钥来恢复以及存储 SharedPreference 中的值

希望这可以帮助..

于 2013-09-30T12:52:47.287 回答
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");
于 2013-09-30T12:57:22.307 回答