我一直试图让这个工作很长一段时间,我已经检查了我能找到的每一个问题,因为这并没有给我任何错误,我没有太多工作要做!基本上,我有一个按钮,它将一个添加到一个int,它显示在它旁边的TextView中。当应用程序关闭时,我希望这个int保持不变,所以看起来共享偏好是我最好的选择(我也考虑过sqlite但是我不确定它是否可以在不更新应用程序的情况下进行编辑,这看起来更简单)。
所以,我的问题是int增加得很好但是当我按下后退按钮/关闭应用程序等时重置为0.
这是所有正在发生的活动。编辑:这是我的代码到目前为止(还有更多的ifs)
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ThirdScreenActivity extends Activity {
public int intPlusOne;
public int intPlusOne2;
public static final String PREFS = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
//Sets the title to recipe name
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
//Sets the correct recipe
if ("ListView Item 1".equals(position)) {
TextView t1 = ((TextView)findViewById(R.id.TextView1));
t1.setText(R.string.string1, null);
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
}
});
}
else if ("ListView Item 2".equals(position)) {
TextView t1= ((TextView)findViewById(R.id.TextView1));
t1.setText(R.string.string2, null);
SharedPreferences sharedPrefs = this.getSharedPreferences("PREFS", MODE_PRIVATE);
intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne2));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne2++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne2));
}
});
}
public void onPause() {
super.onPause();
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt("intPlusOneSaved", intPlusOne);
editor.commit(); //also tried removing this line
editor.putInt("intPlusOne2Saved", IntPlusOne2);
editor.commit();
}
}
如果有帮助,我很乐意提供更多细节!
答案 0 :(得分:1)
这很简单 你犯了一个大错误。
String PlusOneString = String.valueOf(intPlusOne);
Editor editor = sharedPrefs.edit();
editor.putString("PlusOneSaved", PlusOneString);
editor.commit();
此代码位于onCreate中,因此它仅在应用程序启动时保存值,当值递增时,首选项永远不会更新
解决方案:在增加值
时更新首选项 SharedPreferences sharedPrefs;//create it outside on create for accessing in on click event
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
/// IMPORTANT PART SAVE THE VALUE TO PREFERENCES
String PlusOneString = String.valueOf(intPlusOne);
Editor editor = sharedPrefs.edit();
editor.putString("PlusOneSaved", PlusOneString);
editor.commit();
}});
答案 1 :(得分:0)
在活动结束时并不总是调用onDestroy
。您可以尝试将代码放在onPause
方法中:
public void onPause() {
super.onPause();
String PlusOneString = String.valueOf(intPlusOne);
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putString("PlusOneSaved", PlusOneString);
editor.commit();
}
编辑:
您未在onCreate
方法中获取已保存的值。尝试这样的事情:
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ThirdScreenActivity extends Activity {
public int intPlusOne;
public static final String PREFS = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
if ("ListView Item 1".equals(position)) {
TextView tv = ((TextView)findViewById(R.id.TextView1));
tv.setText(R.string.name, null);
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
intPlusOne = sharedPrefs.getInt("PlusOneSaved", 0);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
}
});
}
public void onPause() {
super.onPause();
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt("PlusOneSaved", intPlusOne);
editor.commit();
}
}
编辑2:将这两行放在第一个if
:
intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0);
完整的代码:
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ThirdScreenActivity extends Activity {
public int intPlusOne;
public int intPlusOne2;
public static final String PREFS = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
//Sets the title to recipe name
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0);
//Sets the correct recipe
if ("ListView Item 1".equals(position)) {
TextView t1 = ((TextView)findViewById(R.id.TextView1));
t1.setText(R.string.string1, null);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne));
}
});
}
else if ("ListView Item 2".equals(position)) {
TextView t1= ((TextView)findViewById(R.id.TextView1));
t1.setText(R.string.string2, null);
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne2));
Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
PlusOneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intPlusOne2++;
TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
PlusOne.setText(String.valueOf(intPlusOne2));
}
});
}
public void onPause() {
super.onPause();
SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt("intPlusOneSaved", intPlusOne);
editor.putInt("intPlusOne2Saved", IntPlusOne2);
editor.commit();
}
}