0
我想保存輸入值,以便在應用程序重新啓動時可以調用它們。作爲指導使用this tutorial。我寫了下面的代碼,但沒有成功獲取要回收的值。任何幫助非常感謝你。如何正確實現Android的SharedPreferences?
保存首方法
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();
}
}