- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作金钱计算应用程序,其中一切工作正常,除了每当我从可用余额计算金钱并关闭应用程序并重新打开应用程序时,可用余额都会重置为默认值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/
我的组件的模板呈现一个列表: {{ m.name }} 组件为其设置动画 @Component({ selector: 'app-client-detail', tem
我在回收站 View 中为每个项目使用卡片 View ,但我在图片下方和上方有空白,我不确定如何,我需要摆脱它。这是每个项目的 xml 布局。
我有一个包含许多 float 子元素的 div。每个子项都是一个包含输入和标签的 div。在 Firefox 中它看起来不错,但在 IE7 中,一行的最后一项被换行以适合,而不是被移动到下一行。 Wr
我创建了一个包含三个按钮的 wxpython 框架,在将视频格式化到 wxpanel 上时出现问题 这就是我流式传输网络摄像头的方式。 #code for the stream of webcam t
我想为 ListView 中的每个项目创建一个 fragment ,因为我想分离出一些逻辑。我正在为每个项目使用 View 持有者。如果 View 不存在,我将创建一个新 fragment 并将其添加
我正在使用 Joda 解析 RSS 项中的 pubDate。日期必须采用 RFC-822 格式: http://feed2.w3.org/docs/error/InvalidRFC2822Date.h
我正在尝试为我的第一个项目提供一个在此循环中处于事件状态的类。但是第二个和第三个项目可能没有“事件”类。 @foreach ($news as $newsitem)
我是一名优秀的程序员,十分优秀!