我有一个应用程序,目前无需保存或加载任何类型。我需要它能够保存多达20个不同的变量(字符串/长整数)。
下面是我的.java - 当你看到
时debtAmount2 = (long) extras.getDouble("outstandingBalance");
它有(长),因为它被“放”为双,但我不能用double做一个sharedpref而且不得不让它变长。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
//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);
debtAmount1 = (long) extras.getDouble("outstandingBalance");
String amount = String.valueOf(debtAmount1);
debtAmt1.setText("" + amount);
trigger++;
}
else if (debtNames1 != null) {
debtName1.setText("" + debtNames1);
String amount = String.valueOf(debtAmount1);
debtAmt1.setText("" + amount);
stopper++;
}
if (debtNames2 == null && extras != null && trigger == 0) {
debtNames2 = extras.getString("nickname");
debtName2.setText(debtNames2);
debtAmount2 = (long) extras.getDouble("outstandingBalance");
String amount = String.valueOf(debtAmount2);
debtAmt2.setText("" + amount);
trigger++;
}
else if (debtNames2 != null) {
debtName2.setText("" + debtNames2);
String amount = String.valueOf(debtAmount2);
debtAmt2.setText("" + amount);
stopper++;
}
if (debtNames3 == null && extras != null && trigger == 0) {
debtNames3 = extras.getString("nickname");
debtName3.setText(debtNames3);
debtAmount3 = (long) extras.getDouble("outstandingBalance");
String amount = String.valueOf(debtAmount3);
debtAmt3.setText("" + amount);
trigger++;
}
else if (debtNames3 != null) {
debtName3.setText("" + debtNames3);
String amount = String.valueOf(debtAmount3);
debtAmt3.setText("" + amount);
stopper++;
}
if (debtNames4 == null && extras != null && trigger == 0) {
debtNames4 = extras.getString("nickname");
debtName4.setText(debtNames4);
debtAmount4 = (long) extras.getDouble("outstandingBalance");
String amount = String.valueOf(debtAmount4);
debtAmt4.setText("" + amount);
trigger++;
}
else if (debtNames4 != null) {
debtName4.setText("" + debtNames4);
String amount = String.valueOf(debtAmount4);
debtAmt4.setText("" + amount);
stopper++;
}
if (debtNames5 == null && extras != null && trigger == 0) {
debtNames5 = extras.getString("nickname");
debtName5.setText(debtNames5);
debtAmount5 = (long) extras.getDouble("outstandingBalance");
String amount = String.valueOf(debtAmount5);
debtAmt5.setText("" + amount);
trigger++;
}
else if (debtNames5 != null) {
debtName5.setText("" + debtNames5);
String amount = String.valueOf(debtAmount5);
debtAmt5.setText("" + amount);
stopper++;
}
if (stopper == 6) {
terminate.setText("You have too many debts - get a job!");
}
else {
terminate.setText("");
}
SharedPreferences sharedPref= getSharedPreferences("savedata", 0);
SharedPreferences.Editor editor= sharedPref.edit();
editor.putString("debtNames1", debtNames1);
editor.putLong("debtAmount1", debtAmount1);
editor.putLong("debtRate1", debtRate1);
editor.putInt("debtOrder1", debtOrder1);
editor.putInt("debtTerm1", debtTerm1);
editor.putString("debtNames2", debtNames2);
editor.putLong("debtAmount2", debtAmount2);
editor.putLong("debtRate2", debtRate2);
editor.putInt("debtOrder2", debtOrder2);
editor.putInt("debtTerm2", debtTerm2);
editor.putString("debtNames3", debtNames3);
editor.putLong("debtAmount3", debtAmount3);
editor.putLong("debtRate3", debtRate3);
editor.putInt("debtOrder3", debtOrder3);
editor.putInt("debtTerm3", debtTerm3);
editor.putString("debtNames4", debtNames4);
editor.putLong("debtAmount4", debtAmount4);
editor.putLong("debtRate4", debtRate4);
editor.putInt("debtOrder4", debtOrder4);
editor.putInt("debtTerm4", debtTerm4);
editor.putString("debtNames5", debtNames5);
editor.putLong("debtAmount5", debtAmount5);
editor.putLong("debtRate5", debtRate5);
editor.putInt("debtOrder5", debtOrder5);
editor.putInt("debtTerm5", debtTerm5);
editor.commit();
}
我收到的致命错误已经解决;然而,当从这个活动转移到我正在“获取”我的额外内容时,应用程序不会保留数据。它每次都在取代debtName1。我没有使用菜单链接,而是< - 而不是。
答案 0 :(得分:1)
SharedPreferences.Editor editor= sharedPref.edit();
editor.putString("debtNames1", debtNames1);
editor.putFloat("debtAmount1", 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("debtAmount3", amount3);
editor.putLong("debtRate3", debtRate3);
editor.putInt("debtTerm3", debtTerm3);
editor.putString("debtNames4", debtNames4);
editor.putFloat("debtAmount4", amount4);
editor.putLong("debtRate4", debtRate4);
editor.putInt("debtTerm4", debtTerm4);
editor.putString("debtNames5", debtNames5);
editor.putFloat("debtAmount5", amount5);
editor.putLong("debtRate5", debtRate5);
editor.putInt("debtTerm5", debtTerm5);
editor.putFloat("timeSaved1", timeSaved1);
editor.putFloat("timeSaved2", timeSaved2);
editor.putFloat("timeSaved3", timeSaved3);
editor.putFloat("timeSaved4", timeSaved4);
editor.putFloat("timeSaved5", timeSaved5);
editor.putFloat("totalDebt", totalDebt);
editor.commit();