gpt4 book ai didi

java - 在共享首选项中保存可用余额

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:43 25 4
gpt4 key购买 nike

我正在制作金钱计算应用程序,其中一切工作正常,除了每当我从可用余额计算金钱并关闭应用程序并重新打开应用程序时,可用余额都会重置为默认值2000,而不是为此目的计算的金额,我正在使用共享首选项,但它似乎没有保存该值我的代码用于计算具有共享首选项的按钮单击

  //calculation part

int total = (train_ticket_amount == null || train_ticket_amount.equals("") ? 0:Integer.parseInt(train_ticket_amount))
+ (bus_ticket_amount == null || bus_ticket_amount.equals("")? 0:Integer.parseInt(bus_ticket_amount))
+ (bike_amount == null || bike_amount.equals("")? 0:Integer.parseInt(bike_amount))
+ (share_rickshaw_amount == null || share_rickshaw_amount.equals("")? 0:Integer.parseInt(share_rickshaw_amount));
result = Integer.parseInt(textavailablebalance2) - total;
textAvailableBalance1.setText(Integer.toString(result));
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
result = settings.getInt("price",0);
final_amount = textAvailableBalance1.getText().toString();
//end of calculation part

我也在 onPause 方法上使用共享首选项

 protected void onPause() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("price", result);
editor.apply();
super.onPause();
}

也在 oncreate 中,所以我不明白我哪里出了问题

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work);
initializeContent();
setSpinner();
inputExpenseGiven.setEnabled(false);
imageView.setAdjustViewBounds(true);
imageView2.setAdjustViewBounds(true);
imageView3.setAdjustViewBounds(true);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
result = settings.getInt("price",0);
}

//按钮点击的完整代码,带有onPause和onResume

        public void submit_btn(final View view) {
arrival = inputfrom.getText().toString();
destination = inputTo.getText().toString();
travel_type = spinner.getSelectedItem().toString();
workdescription = inputdescription.getText().toString();
expense_given = checkBoxExpense.getText().toString();
expense_amount = inputExpenseGiven.getText().toString();
train_pass = checkBoxpass.getText().toString();
train_ticket = checkBoxTrain.getText().toString();
train_ticket_amount = inputamount1.getText().toString();
bus_ticket = checkBoxBus.getText().toString();
bus_ticket_amount = inputamount2.getText().toString();
bike = checkBoxBike.getText().toString();
bike_amount = inputamount3.getText().toString();
share_rickshaw = checkBoxShareRickShaw.getText().toString();
share_rickshaw_amount = inputamount4.getText().toString();
textavailablebalance2 = textAvailableBalance1.getText().toString();

//calculation part

// Save the calculated result price instead of picking the old one

int total = (train_ticket_amount == null || train_ticket_amount.equals("") ? 0:Integer.parseInt(train_ticket_amount))
+ (bus_ticket_amount == null || bus_ticket_amount.equals("")? 0:Integer.parseInt(bus_ticket_amount))
+ (bike_amount == null || bike_amount.equals("")? 0:Integer.parseInt(bike_amount))
+ (share_rickshaw_amount == null || share_rickshaw_amount.equals("")? 0:Integer.parseInt(share_rickshaw_amount));
result = Integer.parseInt(textavailablebalance2) - total;


if(!expense_amount.isEmpty())
result+=Integer.parseInt(expense_amount);
textAvailableBalance1.setText(Integer.toString(result));

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("price", result);
editor.apply();


//second calculation
amountused = (train_ticket_amount == null || train_ticket_amount.equals("") ? 0:Integer.parseInt(train_ticket_amount))+
(bus_ticket_amount == null || bus_ticket_amount.equals("")? 0:Integer.parseInt(bus_ticket_amount))+
(bike_amount == null || bike_amount.equals("")? 0:Integer.parseInt(bike_amount))+
(share_rickshaw_amount == null || share_rickshaw_amount.equals("")? 0:Integer.parseInt(share_rickshaw_amount));
textfinalamount.setText(Integer.toString(amountused));
amountused1 = textfinalamount.getText().toString();

//xml

<TextView
android:id="@+id/textAvailableBalance1"
android:layout_width="95dp"
android:layout_height="26dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="2000"
app:layout_constraintBottom_toTopOf="@+id/btn_submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.514"
app:layout_constraintStart_toEndOf="@+id/textAvailableBalance" />

最佳答案

只需删除 onPause() 方法中的代码

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("price", result);
editor.apply();

也来自onResume()方法

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
result = settings.getInt("price",0)

计算完成后存储结果就足够了

如果你想在 TextView 中显示最新的结果,

onCreate()中添加这一行

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
result = settings.getInt("price",2000);//default result 2000
textAvailableBalance1.setText(String.valueOf(result));

问题是,您没有将 SharedPref 中存储的文本设置为您的 TextView

编辑:

您可以使用此代码,

         int total = (train_ticket_amount == null || train_ticket_amount.equals("") ? 0:Integer.parseInt(train_ticket_amount))
+ (bus_ticket_amount == null || bus_ticket_amount.equals("")? 0:Integer.parseInt(bus_ticket_amount))
+ (bike_amount == null || bike_amount.equals("")? 0:Integer.parseInt(bike_amount))
+ (share_rickshaw_amount == null || share_rickshaw_amount.equals("")? 0:Integer.parseInt(share_rickshaw_amount));
result = Integer.parseInt(textavailablebalance2) - total;


if(!expense_amount.isEmpty())
result+=Integer.parseInt(expense_amount));
textAvailableBalance1.setText(Integer.toString(result));

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("price", result);
editor.apply();


//second calculation
amountused = (train_ticket_amount == null || train_ticket_amount.equals("") ? 0:Integer.parseInt(train_ticket_amount))+
(bus_ticket_amount == null || bus_ticket_amount.equals("")? 0:Integer.parseInt(bus_ticket_amount))+
(bike_amount == null || bike_amount.equals("")? 0:Integer.parseInt(bike_amount))+
(share_rickshaw_amount == null || share_rickshaw_amount.equals("")? 0:Integer.parseInt(share_rickshaw_amount));
textfinalamount.setText(Integer.toString(amountused));
amountused1 = textfinalamount.getText().toString();

关于java - 在共享首选项中保存可用余额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606770/

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