SharedPreferences 整数加法
SharedPreferences integer addition
我有一个代码...它的保存数据和加载数据非常好,但是...当我重置应用程序时,我的分数正在加载,但是当我单击 +5 分数的按钮时,我的分数重置并设置为 5。我想要那个加法 +5,但它不起作用...
我理解加法的问题,因为保存和加载工作很好,但加法不起作用。
抱歉我的英语不好:)
int mCounts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appli);
Settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
score = (TextView) findViewById(R.id.score);
score.setText(String.valueOf(mCounts));
}
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
}
public void onPause() {
super.onPause();
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
在您的按钮中试试这个代码:
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
问题在这里:
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
删除int
。它会起作用
我有一个代码...它的保存数据和加载数据非常好,但是...当我重置应用程序时,我的分数正在加载,但是当我单击 +5 分数的按钮时,我的分数重置并设置为 5。我想要那个加法 +5,但它不起作用...
我理解加法的问题,因为保存和加载工作很好,但加法不起作用。
抱歉我的英语不好:)
int mCounts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appli);
Settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
score = (TextView) findViewById(R.id.score);
score.setText(String.valueOf(mCounts));
}
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
}
public void onPause() {
super.onPause();
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
在您的按钮中试试这个代码:
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
问题在这里:
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
删除int
。它会起作用