作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将数据存储在我的应用程序类中
public class VApp extends Application {
@Override
public void onCreate() {
super.onCreate();
}
public Double getGrandTotal() {
return grandTotal;
}
public Double setGrandTotal(Double grandTotal) {
this.grandTotal = grandTotal;
return grandTotal;
}
public Double grandTotal = 0.0;
}
我在其他 Activity 中使用 setGrandTotal() 和 getGrandTotal() 方法来存储数据,它的工作正常,我的问题是当我关闭应用程序重新开始时总计变为 0.0,我想在第二次打开应用程序时存储总计数据。
最佳答案
public Double getGrandTotal() {
if (grandTotal == 0) {
SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
String value = sharedPreferences.getString("grandTotal", "0");
grandTotal = Double.valueOf(value);
}
return grandTotal;
}
public Double setGrandTotal(Double grandTotal) {
this.grandTotal = grandTotal;
SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("grandTotal", String.valueOf(grandTotal)).apply();
return grandTotal;
}
public Double grandTotal = 0.0;
关于android - 第二次打开应用程序时将数据存储在应用程序类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36785563/
我是一名优秀的程序员,十分优秀!