Android SharedReference的问题
我一直在努力让这个工作了很长一段时间,我已经检查了每一个问题,我可以找到,因为这不会给我任何错误,我没有太多的工作!基本上,我有一个按钮,可以将一个添加到int中,int显示在它旁边的文本视图中。我希望这个int在应用程序关闭时保持不变,所以看起来SharedReferences是我最好的选择(我也考虑过sqlite,但我不确定它是否可以在不更新应用程序的情况下进行编辑,这看起来更简单) 所以,我的问题是int值增加得很好,但当我按下后退按钮/关闭应用程序时,int值重置为0 这就是这一切发生的活动。编辑:这是到目前为止我的代码(还有更多其他ifs) } 如果有帮助的话,我很乐意提供更多的细节 这很简单 你犯了个大错Android SharedReference的问题,android,int,sharedpreferences,Android,Int,Sharedpreferences,我一直在努力让这个工作了很长一段时间,我已经检查了每一个问题,我可以找到,因为这不会给我任何错误,我没有太多的工作!基本上,我有一个按钮,可以将一个添加到int中,int显示在它旁边的文本视图中。我希望这个int在应用程序关闭时保持不变,所以看起来SharedReferences是我最好的选择(我也考虑过sqlite,但我不确定它是否可以在不更新应用程序的情况下进行编辑,这看起来更简单) 所以,我的问题是int值增加得很好,但当我按下后退按钮/关闭应用程序时,int值重置为0 这就是这一切发生的
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();
}});
并非总是在活动结束时调用
ondestory
。您可以尝试将代码放入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:如果,则将这两行放在第一行之前:
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();
}
}
还有一件事,你也可以用“putin&getInt”来代替“putString&getString”,我试过getInt,但没用,想必我也弄糟了!那么末尾的编辑器行会进入onClick内部吗?(感谢您的快速回复)好的,我尝试了这个,当我在应用程序中切换页面时,它非常有效。但是,如果我关闭应用程序(通过按下第一页上的“后退”按钮),它似乎会重置它,当我返回时,它会返回到0!尝试记录sharedPrefs.getInt的值(“PlusOneSaved”,0);并检查它是否保存,如果保存正确,则您的活动没有在应用程序启动时读取值。每次更改值和首选项后,都可以使用log检查代码。另外,我想知道您为什么使用这个if代码-if(“ListView项目1”。equals(position))该代码是因为上一个活动是ListView,这是您单击项目时显示的页面。这将在单击时将标题和其他功能设置为正确的值,因为我在“活动”中有其他“else if”语句,但我删除了它们以便于回答此问题。回到这个问题,Addrallyn的答案完全解决了我的问题,因此我将他的答案标记为答案,尽管感谢您的快速回复和帮助,如果我继续关注的话,可能会很有效。非常感谢,您已经解决了这个问题!我不确定是onDestroy/onPause修复程序还是其他修复程序,但我为此花了很多时间,非常感谢。您没有在
onCreate
方法中初始化intPlusOne
属性。嗯,我还有一个小问题,但我不确定是否要再问一个问题,或者这是一个可以很快解决的问题。我有不止一个“如果x等于位置”(使用else if),即使在应用程序关闭的情况下,它们各自也能正常工作,但如果我+1一项,返回并+1另一项,第一项将重置为0!你能帮忙吗?(请参见编辑的问题)如果if
,您可以发布您的else吗?这是if
的直接复制和粘贴,只是更改了变量名。不过我还是要把它编辑成问题
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();
}
}