保存数据并使用它

时间:2013-05-01 20:56:17

标签: android

我要做的是在活动中保存一些数字,以便在活动重新开始时我可以将它们添加到更多数字。

我尝试在暂停时保存数字,然后将它们添加到Resume上的新数字中,它有点工作但是当我旋转手机时它会再次添加保存的数字,这是一个更好的方法来解决这个问题

一些代码

    if (getIntent().hasExtra("ResultBl100")) {
        editbl.setText(bl100 + "");

        numberOfBl = Integer.parseInt(editbl.getText().toString());
        totalSd = (float) (numberOfBl * 0.0024);
        editsd.setText(String.format("%.1f", totalSd));

        b = Float.parseFloat(editsd.getText().toString());
        totalCt = (int) Math.ceil(b * 7);
        ct.setText(Integer.toString(totalCt));
    }

    //sending back to start of app
    addAgain = (Button) findViewById(R.id.add_again);
    addAgain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Amounts.this,
                ChooseMaterials.class);
            intent.putExtra("AddMore", ADD_MORE);
            startActivity(intent);
        } 
    }); 


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    if (TextUtils.isEmpty(editBl.getText().toString())) {
        numberOfBl = 0;
    } else {
        numberOfBl = Integer.parseInt(editBl.getText().toString();
    }
    if (TextUtils.isEmpty(editSd.getText().toString())) {
        amountSd = 0;
    } else {
        amountSd = Float.parseFloat(editSd.getText().toString());
    }
    SharedPreferences prefs = getSharedPreferences(
            "SavedTotals", Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("savedBl", numberOfBl);
    editor.putFloat("savedSd", amountSd);
    editor.commit();
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences preferences = getSharedPreferences("SavedTotals",
        Context.MODE_PRIVATE);

    int blSaved = preferences.getInt("savedBl", 0);
    float sdSaved = preferences.getFloat("savedSd", 0);

    int numberBl = blSaved + numberOfBl;
    editBl.setText(Integer.toString(numberBl));

    float amountSds = sdSaved + amountSd;
    editSd.setText(Float.toString(amountSds));
}

Drawing of Activities

我仍然坚持这个可能有人请给我一些帮助,iv附上一张照片来展示我想要实现的目标。

用户在菜单上开始,这会导致活动ab或c,他们输入一些数字并最终显示最终活动,其中显示一些总计,当用户按下上一个活动的按钮时,我想保存总计和将用户返回到菜单,他可以重新开始,当用户结束最后一次活动时,会有一组新的总计我希望将保存的总数添加到新的总计中并显示它们

Iv现在尝试了保存实例状态和恢复实例状态,并且我在旋转手机时遇到了同样的问题,它再次添​​加了保存的总计

3 个答案:

答案 0 :(得分:2)

将您的值保存在一个包中,我可以在一秒钟内为您提供代码。

答案 1 :(得分:1)

写值示例

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInsta nceState);

    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.
    savedInstanceState.putBoolean("MyBoolean", true);
    savedInstanceState.putDouble("myDouble", 1.9);
    savedInstanceState.putInt("MyInt", 1);
    savedInstanceState.putString("MyString", "Welcome back to Android");
    // etc.
}

提取值示例

@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedIn stanceState);
    // Restore UI state from the savedInstanceState.
    // This bundle has also been passed to onCreate.
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
    double myDouble = savedInstanceState.getDouble("myDouble");
    int myInt = savedInstanceState.getInt("MyInt");
    String myString = savedInstanceState.getString("MyString");
}

答案 2 :(得分:-1)

您可以通过this回答检测设备轮换。检测到旋转时,在onResume()和onPause()中禁用您的函数。希望这会有所帮助。