1

我有一组变量(浮点数/整数/字符串)保存在共享首选项中。当我第一次运行它时,所有 3 条数据都完美显示 - 当我第二次运行它时,它保持 debtNames1(见下文),但对于 amount1,它将它设置为 0.0。我已经检查了脚本,并尝试了 long/string 并且它做同样的事情。以下是活动:

public class DebtList extends Activity {

    String debtNames1, debtNames2, debtNames3, debtNames4, debtNames5, debtNames6 = null; 
    float amount1, amount2, amount3, amount4, amount5, amount6;
    long debtRate1, debtRate2, debtRate3, debtRate4, debtRate5, debtRate6;
    int debtTerm1, debtTerm2, debtTerm3, debtTerm4, debtTerm5, debtTerm6;

    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.debtlist);
        SharedPreferences sharedPref= getSharedPreferences("chaosdata", 0);
        debtNames1 = sharedPref.getString("debtNames1", debtNames1);
        debtNames2 = sharedPref.getString("debtNames2", debtNames2);
        debtNames3 = sharedPref.getString("debtNames3", debtNames3);
        debtNames4 = sharedPref.getString("debtNames4", debtNames4);
        debtNames5 = sharedPref.getString("debtNames5", debtNames5);

        amount1 = sharedPref.getFloat("debtAmount1", amount1);
        amount2 = sharedPref.getFloat("debtAmount2", amount2);
        amount3 = sharedPref.getFloat("debtAmount3", amount3);
        amount4 = sharedPref.getFloat("debtAmount4", amount4);
        amount5 = sharedPref.getFloat("debtAmount5", amount5);

        debtRate1 = sharedPref.getLong("debtRate1", debtRate1);
        debtRate2 = sharedPref.getLong("debtRate2", debtRate2);
        debtRate3 = sharedPref.getLong("debtRate3", debtRate3);
        debtRate4 = sharedPref.getLong("debtRate4", debtRate4);
        debtRate5 = sharedPref.getLong("debtRate5", debtRate5);

        debtTerm1 = sharedPref.getInt("debtTerm1", debtTerm1);
        debtTerm2 = sharedPref.getInt("debtTerm2", debtTerm2);
        debtTerm3 = sharedPref.getInt("debtTerm3", debtTerm3);
        debtTerm4 = sharedPref.getInt("debtTerm4", debtTerm4);
        debtTerm5 = sharedPref.getInt("debtTerm5", debtTerm5);

        //Name Displays
        TextView debtName1 = (TextView) findViewById(R.id.dispName1);
        TextView debtName2 = (TextView) findViewById(R.id.dispName2);
        TextView debtName3 = (TextView) findViewById(R.id.dispName3);
        TextView debtName4 = (TextView) findViewById(R.id.dispName4);
        TextView debtName5 = (TextView) findViewById(R.id.dispName5);

        //Amount Displays
        TextView debtAmt1 = (TextView) findViewById(R.id.dispBal1);
        TextView debtAmt2 = (TextView) findViewById(R.id.dispBal2);
        TextView debtAmt3 = (TextView) findViewById(R.id.dispBal3);
        TextView debtAmt4 = (TextView) findViewById(R.id.dispBal4);
        TextView debtAmt5 = (TextView) findViewById(R.id.dispBal5);

        //Order Displays
        TextView dispDebtOrder1 = (TextView) findViewById(R.id.dispPayOrder1);
        TextView dispDebtOrder2 = (TextView) findViewById(R.id.dispPayOrder2);
        TextView dispDebtOrder3 = (TextView) findViewById(R.id.dispPayOrder3);
        TextView dispDebtOrder4 = (TextView) findViewById(R.id.dispPayOrder4);
        TextView dispDebtOrder5 = (TextView) findViewById(R.id.dispPayOrder5);

        TextView terminate = (TextView) findViewById(R.id.terminate);

        Integer trigger = 0;
        Integer stopper = 1;
        Bundle extras = getIntent().getExtras();

        if (debtNames1 == null && extras != null && trigger == 0) {
            debtNames1 = extras.getString("nickname");
            debtName1.setText("" + debtNames1);
            amount1 = extras.getFloat("outstandingBalance");
            debtAmt1.setText("" + (float) amount1);
            dispDebtOrder1.setText("1");
            trigger++;
        }
        else if (debtNames1 != null) {
            debtName1.setText("" + debtNames1);
            debtAmt1.setText("" + (float) amount1);
            dispDebtOrder1.setText("1");
            stopper++;
        }

        if (debtNames2 == null && extras != null && trigger == 0) {
            debtNames2 = extras.getString("nickname");
            debtName2.setText(debtNames2);
            amount2 = extras.getFloat("outstandingBalance");
            debtAmt2.setText("" + amount2);
            dispDebtOrder2.setText("2");
            trigger++;
        }
        else if (debtNames2 != null) {
            debtName2.setText("" + debtNames2);
            debtAmt2.setText("" + amount2);
            dispDebtOrder2.setText("2");
            stopper++;
        }

        if (debtNames3 == null && extras != null && trigger == 0) {
            debtNames3 = extras.getString("nickname");
            debtName3.setText(debtNames3);
            amount3 = extras.getFloat("outstandingBalance");
            debtAmt3.setText("" + amount3);
            dispDebtOrder3.setText("3");
            trigger++;
        }
        else if (debtNames3 != null) {
            debtName3.setText("" + debtNames3);
            debtAmt3.setText("" + amount3); 
            dispDebtOrder3.setText("3");
            stopper++;
        }

        if (debtNames4 == null && extras != null && trigger == 0) {
            debtNames4 = extras.getString("nickname");
            debtName4.setText(debtNames4);
            amount4 = extras.getFloat("outstandingBalance");
            debtAmt4.setText("" + amount4);
            dispDebtOrder4.setText("4");
            trigger++;
        }
        else if (debtNames4 != null) {
            debtName4.setText("" + debtNames4);
            debtAmt4.setText("" + amount4);
            dispDebtOrder4.setText("4");
            stopper++;
        }

        if (debtNames5 == null && extras != null && trigger == 0) {
            debtNames5 = extras.getString("nickname");
            debtName5.setText(debtNames5);
            amount5 = extras.getFloat("outstandingBalance");
            debtAmt5.setText("" + amount5);
            dispDebtOrder5.setText("5");
            trigger++;
        }
        else if (debtNames5 != null) {
            debtName5.setText("" + debtNames5);
            debtAmt5.setText("" + amount5); 
            dispDebtOrder5.setText("5");
            stopper++;
        }

        if (stopper == 6) {
            terminate.setText("Maximum Debts Reached!");
            }
        else {
            terminate.setText("");
        }

        SharedPreferences.Editor editor= sharedPref.edit();

        editor.putString("debtNames1", debtNames1);
        editor.putFloat("debtAmount2", amount1);
        editor.putLong("debtRate1", debtRate1);
        editor.putInt("debtTerm1", debtTerm1);

        editor.putString("debtNames2", debtNames2);
        editor.putFloat("debtAmount2", amount2);
        editor.putLong("debtRate2", debtRate2);
        editor.putInt("debtTerm2", debtTerm2);

        editor.putString("debtNames3", debtNames3);
        editor.putFloat("debtAmount2", amount3);
        editor.putLong("debtRate3", debtRate3);
        editor.putInt("debtTerm3", debtTerm3);

        editor.putString("debtNames4", debtNames4);
        editor.putFloat("debtAmount2", amount4);
        editor.putLong("debtRate4", debtRate4);
        editor.putInt("debtTerm4", debtTerm4);

        editor.putString("debtNames5", debtNames5);
        editor.putFloat("debtAmount2", amount5);
        editor.putLong("debtRate5", debtRate5);
        editor.putInt("debtTerm5", debtTerm5);

        editor.commit();
    }

正如我所说,当我“获取”数据时,它会在第一个 if 中完美显示。我第二次“获取”数据时,它在第二个 if 中完美显示,但第一次 if 只正确显示了 debilityNames1,而不是 amount1。

有任何想法吗?

谢谢!

4

1 回答 1

1

请检查语句'editor.putFloat("debtAmount2", amount1);'的键值

在所有情况下,您都使用“debtAmount2”;

于 2013-10-24T04:13:15.753 回答