我想保存输入值,以便在应用程序重新启动时可以调用它们。使用本教程作为指南。我编写了以下代码,但没有成功获取要调用的值。非常感谢您的任何帮助。
保存首选项方法
public void SavePreferences(String key, String value){
SharedPreferences portfoliopreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = portfoliopreferences.edit();
editor.putString(key, value);
editor.commit();
}
保存首选项输入
if(users.contains(usernull)){
pn1 = enterportfolioname.getText().toString();
denomination1 = denomination;
portfoliocurrency1 = portfoliocurrency;
user1 = new PortfolioRecord(pn1, denomination1+df.format(portfoliovalue1));
users.remove(usernull);
users.add(user1);
portfoliosdelete.add(pn1);
adapterdeletetype.notifyDataSetChanged();
portfoliolist.invalidateViews();
SavePreferences("U1C", "ýes");
SavePreferences("PN1", enterportfolioname.getText().toString());
SavePreferences("DN1", denomination);
SavePreferences("PC1", "usd");
}
加载首选项
public void LoadPreferences(){
SharedPreferences portfoliopreferences = getPreferences(MODE_PRIVATE);
String isuser1created = portfoliopreferences.getString("U1C", "");
if(isuser1created.equals("yes")){
String savedportfolioname = portfoliopreferences.getString("PN1", "");
String saveddenomination = portfoliopreferences.getString("DN1", "");
String savedporfoliocurrency = portfoliopreferences.getString("PC1", "");
pn1 = savedportfolioname;
denomination1 = saveddenomination;
portfoliocurrency1 = currencyUSD;
user1 = new PortfolioRecord(pn1, denomination1+portfoliovalue1);
users.add(user1);
portfoliosdelete.add(pn1);
calculateportfoliovalue1();
adapterdeletetype.notifyDataSetChanged();
portfoliolist.invalidateViews();
}
}