gpt4 book ai didi

android - 第二次打开应用程序时将数据存储在应用程序类中

转载 作者:行者123 更新时间:2023-11-30 01:23:15 25 4
gpt4 key购买 nike

我将数据存储在我的应用程序类中

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com