如何在应用程序上保存值关闭并在应用程序再次打开时检索

时间:2013-09-23 11:18:10

标签: android sharedpreferences

我希望在应用程序关闭时保存此游戏中的totalBalance,并在用户再次播放应用程序时将其再次检索到textView5 ...但我正在尝试并且它没有给我任何东西请帮助我...我需要什么在这做...

public class Main extends Activity implements OnClickListener {

private ArrayList<ToggleButton> mToggleButtons = new ArrayList<ToggleButton>();
public static TextView textView1, textView2, textView3, textView4,
        textView5, textView6;
Button button1;
ToggleButton t;
int i;
int randomNumber;
float winingPrize = 0;
float totalBalance = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    textView1 = (TextView) findViewById(R.id.textView1);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView3 = (TextView) findViewById(R.id.textView3);
    textView4 = (TextView) findViewById(R.id.textView4);
    textView5 = (TextView) findViewById(R.id.textView5);
    textView6 = (TextView) findViewById(R.id.textView6);

    button1 = (Button) findViewById(R.id.button1);

    ToggleButton cb = null;

    for (i = 1; i <= 9; i++) {
        cb = new ToggleButton(this);
        cb.setText(Integer.toString(i));
        cb.setTextOn(Integer.toString(i));
        cb.setTextOff(Integer.toString(i));
        cb.setBackgroundResource(R.drawable.check);
        cb.setOnClickListener(this);
        cb.setId(i);
        registerForContextMenu(cb);
        mToggleButtons.add(cb);
    }

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new CustomAdapter(mToggleButtons));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Button b1 = (Button) findViewById(R.id.button1);
            // TextView textView2 = (TextView) findViewById(R.id.textView1);
            // textView2.setText(Integer.toString(i));

        }
    });
}

@Override
public void onClick(View v) {

    // Making a array list and when we click on button1 its checkes
    // which of the togglebuttons are selected
    final ArrayList<Integer> positive_IDs = new ArrayList<Integer>();
    for (int i = 0; i < mToggleButtons.size(); i++) {
        ToggleButton b = mToggleButtons.get(i);
        if (b.isChecked()) {
            // putting all buttons id that are checked into
            // arrayList
            positive_IDs.add(b.getId());
        }
    }
    textView6.setText("Selected Numbers : " + positive_IDs.toString());
    // Toast.makeText(getBaseContext(), positive_IDs.toString() +
    // " Enabled", Toast.LENGTH_SHORT).show();

    textView3.setText("Total Number Selected = "
            + Integer.toString(positive_IDs.size()));

    switch (positive_IDs.size()) {
    case 1: {
        winingPrize = .5f;
        break;
    }
    case 2: {
        winingPrize = .25f;
        break;
    }
    case 3: {
        winingPrize = .1f;
        break;
    }
    case 4: {
        winingPrize = .05f;
        break;
    }
    case 5: {
        winingPrize = .01f;
        break;
    }
    case 6: {
        winingPrize = .00f;
        break;
    }
    }
    textView4.setText("Wining Prize = Rs: "
            + String.format("%.2f", winingPrize));

    final Button button = (Button) findViewById(R.id.button1);
    button.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));
            button.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");
                // Toast.makeText(getBaseContext(), "Number:" +
                // Integer.toString(randomNumber) + " Matched",
                // Toast.LENGTH_SHORT).show();

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

            }

        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

有许多方法可以实现这一目标,其中一些是

使用静态变量或

使用Shared preferences

请参阅此link for more options

我建议您使用共享首选项来存储数据。

答案 1 :(得分:0)

你可以使用像这样的共享偏好方法

public class PreferenceData {

    public static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    public static void setTempString(Context ctx, String str, int str1) {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putInt(str, str1);
        editor.commit();
    }

    public static int getTempString(Context ctx, String str) {
        return getSharedPreferences(ctx).getInt(str, 100);
    }

    public static void setString(Context ctx, String str, String str1) {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(str, str1);
        editor.commit();
    }

在一个类中,并在需要时在函数中检索它:

int prfdata = PreferenceData.getTempString(context,
                                    "downloadcount");
                            prfdata = prfdata--;
                            PreferenceData.setTempString(context,
                                    "downloadcount", prfdata);