Android中的共享首选项错误 - 所有保存的值都是相同的
我正在制作一个简单的计数器应用程序,并且我希望在该应用程序关闭时保存我的计数。 目前,分配给保存变量的最后一个计数写入所有保存变量:editor.putitring(a_count,string.valueof(acount));
也就是说,当我打开时关闭后,我的所有计数都显示了acount的最后值。如果将最后一行更改为editor.putString(f_count,string.valueof(fcount));
,则将fcount的值保存到所有变量中。 知道为什么会发生这种情况吗?我正在使用整数变量保持计数值,我必须使用转换方法将它们来回传递给主要活动中的文本视图...以这种方式增加它们会更容易,但是我敢肯定有更好的方法做到这一点。我也确定这可能会以某种方式弄乱事情。 感谢任何建议!
这是我的代码。我的猜测是,我在我的savedata()方法中犯了一个错误:
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public static String SHARED_PREFS = "sharedPrefs";
public static String F_COUNT = "0";
public static String Z_COUNT = "0";
public static String A_COUNT = "0";
public static String M_COUNT = "0";
public static String I_COUNT = "0";
private static int fCount = 0;
private static int zCount = 0;
private static int aCount = 0;
private static int mCount = 0;
private static int iCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadData();
updateViews();
}
public void saveData(){ //putting int counts into string local data
sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putString(F_COUNT, String.valueOf(fCount));
editor.putString(Z_COUNT, String.valueOf(zCount));
editor.putString(I_COUNT, String.valueOf(iCount));
editor.putString(M_COUNT, String.valueOf(mCount));
editor.putString(A_COUNT, String.valueOf(aCount));
editor.apply();
Toast.makeText(this, String.valueOf(mCount), Toast.LENGTH_LONG).show();
}
public void loadData(){ //loading string local data into int counts
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
fCount = Integer.parseInt(sharedPreferences.getString(F_COUNT, "0"));
zCount = Integer.parseInt(sharedPreferences.getString(Z_COUNT, "0"));
aCount = Integer.parseInt(sharedPreferences.getString(A_COUNT, "0"));
iCount = Integer.parseInt(sharedPreferences.getString(I_COUNT, "0"));
mCount = Integer.parseInt(sharedPreferences.getString(M_COUNT, "0"));
Toast.makeText(this, F_COUNT, Toast.LENGTH_LONG).show();
}
public void updateViews(){ //settings int counts to textViews
TextView fNum = findViewById(R.id.fNum12);
fNum.setText(String.valueOf(fCount));
TextView zNum = findViewById(R.id.fNum7);
zNum.setText(String.valueOf(zCount));
TextView aNum = findViewById(R.id.fNum14);
aNum.setText(String.valueOf(aCount));
TextView iNum = findViewById(R.id.fNum17);
iNum.setText(String.valueOf(iCount));
TextView mNum = findViewById(R.id.fNum13);
mNum.setText(String.valueOf(mCount));
}
I'm making a simple counter app and I'd like for my counts to save when the app closes.
At the moment, the last count assigned to the saving variable is written to all the saving variables: editor.putString(A_COUNT, String.valueOf(aCount));
That is to say, when I open the app after closing it, all of my counts show the last value of aCount. If I change the last line to editor.putString(F_COUNT, String.valueOf(fCount));
, then the value of fCount is saved to all of the variables.
Any idea why this is happening? I'm using integer variables to hold the count values and I have to use conversion methods to pass them back and forth to the TextViews in the main activity... it's easier to increment them that way but I'm sure there's a better way to do it. I'm also sure this could mess things up in some way.
I'd appreciate any advice!
Here's me code. My guess is that I've made a mistake in my saveData() method:
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public static String SHARED_PREFS = "sharedPrefs";
public static String F_COUNT = "0";
public static String Z_COUNT = "0";
public static String A_COUNT = "0";
public static String M_COUNT = "0";
public static String I_COUNT = "0";
private static int fCount = 0;
private static int zCount = 0;
private static int aCount = 0;
private static int mCount = 0;
private static int iCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadData();
updateViews();
}
public void saveData(){ //putting int counts into string local data
sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putString(F_COUNT, String.valueOf(fCount));
editor.putString(Z_COUNT, String.valueOf(zCount));
editor.putString(I_COUNT, String.valueOf(iCount));
editor.putString(M_COUNT, String.valueOf(mCount));
editor.putString(A_COUNT, String.valueOf(aCount));
editor.apply();
Toast.makeText(this, String.valueOf(mCount), Toast.LENGTH_LONG).show();
}
public void loadData(){ //loading string local data into int counts
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
fCount = Integer.parseInt(sharedPreferences.getString(F_COUNT, "0"));
zCount = Integer.parseInt(sharedPreferences.getString(Z_COUNT, "0"));
aCount = Integer.parseInt(sharedPreferences.getString(A_COUNT, "0"));
iCount = Integer.parseInt(sharedPreferences.getString(I_COUNT, "0"));
mCount = Integer.parseInt(sharedPreferences.getString(M_COUNT, "0"));
Toast.makeText(this, F_COUNT, Toast.LENGTH_LONG).show();
}
public void updateViews(){ //settings int counts to textViews
TextView fNum = findViewById(R.id.fNum12);
fNum.setText(String.valueOf(fCount));
TextView zNum = findViewById(R.id.fNum7);
zNum.setText(String.valueOf(zCount));
TextView aNum = findViewById(R.id.fNum14);
aNum.setText(String.valueOf(aCount));
TextView iNum = findViewById(R.id.fNum17);
iNum.setText(String.valueOf(iCount));
TextView mNum = findViewById(R.id.fNum13);
mNum.setText(String.valueOf(mCount));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论