2013-10-24 48 views
1

我有一組varials(float/integer/string)被保存在sharedpreferences中。當我第一次運行它時,所有3個數據都完美顯示 - 當我第二次運行它時,它保留debtNames1(見下文),但對於amount1,它將其置於0.0。我已經閱讀了腳本,並且嘗試了很長的字符串,並且它也做了同樣的事情。下面是活動:sharedpreference不保存/加載數據

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。 第二次我「獲取」數據,它顯示在第二個如果,但第一個如果只顯示債務Names1正確,而不是金額1。

任何想法?

謝謝!

+0

試試這種方式: http://stackoverflow.com/questions/19492710/how-to-use-sharedpreferences-outside-of-oncreate/19492831#19492831 –

回答

1

請檢查語句'editor.putFloat(「debtAmount2」,amount1)的關鍵值;''

在所有情況下,您正在使用'debtAmount2';