Android SharedPreferences 所有变量分配给相同的值
我正在创建一个包含多项活动的应用程序。 SharedPreferences 在多个活动中读取。
我发现当我更新 SharedPreferences 文件之一中的单个变量时,该文件中的所有变量都会采用分配的值。
发生这种情况有一般原因吗?如果没有的话我可以发布代码。
以下代码用于写入和读取值(全部大写的任何内容都是唯一的整数常量)
public void LevelUp(int gameType) {
step++;
SharedPreferences settings = getSharedPreferences("Steps", 0);
SharedPreferences.Editor editor = settings.edit();
switch (gameType) {
case NUMBERS_SPEED: editor.putInt("NUMBERS_SPEED", step);
case NUMBERS_BINARY: editor.putInt("NUMBERS_BINARY", step);
case NUMBERS_SPOKEN: editor.putInt("NUMBERS_SPOKEN", step);
case LISTS_WORDS: editor.putInt("LISTS_WORDS", step);
case LISTS_EVENTS: editor.putInt("LISTS_EVENTS", step);
case SHAPES_FACES: editor.putInt("SHAPES_FACES", step);
case SHAPES_ABSTRACT: editor.putInt("SHAPES_ABSTRACT", step);
case CARDS_SPEED: editor.putInt("CARDS_SPEED", step);
}
editor.commit();
}
public int getStep(int gameType) {
SharedPreferences settings = getSharedPreferences("Steps", 0);
switch (gameType) {
case NUMBERS_SPEED: return settings.getInt("NUMBERS_SPEED", 1);
case NUMBERS_BINARY: return settings.getInt("NUMBERS_BINARY", 1);
case NUMBERS_SPOKEN: return settings.getInt("NUMBERS_SPOKEN", 1);
case LISTS_WORDS: return settings.getInt("LISTS_WORDS", 1);
case LISTS_EVENTS: return settings.getInt("LISTS_EVENTS", 1);
case SHAPES_FACES: return settings.getInt("SHAPES_FACES", 1);
case SHAPES_ABSTRACT: return settings.getInt("SHAPES_ABSTRACT", 1);
case CARDS_SPEED: return settings.getInt("CARDS_SPEED", 1);
default: return -1;
}
}
以下代码驻留在不同的活动中:
SharedPreferences settings = getSharedPreferences("Steps", 0);
step = settings.getInt("NUMBERS_SPOKEN", 1);
I am creating an app with several activities. SharedPreferences are read in multiple activities.
I have found that when I update a single variable in one of the SharedPreferences files, all of the variables in that file take on the value assigned.
Is there a general reason why this might occur? If not, I can post code.
THE FOLLOWING CODE IS USED TO WRITE AND READ THE VALUES (anything in all caps is a unique integer constant)
public void LevelUp(int gameType) {
step++;
SharedPreferences settings = getSharedPreferences("Steps", 0);
SharedPreferences.Editor editor = settings.edit();
switch (gameType) {
case NUMBERS_SPEED: editor.putInt("NUMBERS_SPEED", step);
case NUMBERS_BINARY: editor.putInt("NUMBERS_BINARY", step);
case NUMBERS_SPOKEN: editor.putInt("NUMBERS_SPOKEN", step);
case LISTS_WORDS: editor.putInt("LISTS_WORDS", step);
case LISTS_EVENTS: editor.putInt("LISTS_EVENTS", step);
case SHAPES_FACES: editor.putInt("SHAPES_FACES", step);
case SHAPES_ABSTRACT: editor.putInt("SHAPES_ABSTRACT", step);
case CARDS_SPEED: editor.putInt("CARDS_SPEED", step);
}
editor.commit();
}
public int getStep(int gameType) {
SharedPreferences settings = getSharedPreferences("Steps", 0);
switch (gameType) {
case NUMBERS_SPEED: return settings.getInt("NUMBERS_SPEED", 1);
case NUMBERS_BINARY: return settings.getInt("NUMBERS_BINARY", 1);
case NUMBERS_SPOKEN: return settings.getInt("NUMBERS_SPOKEN", 1);
case LISTS_WORDS: return settings.getInt("LISTS_WORDS", 1);
case LISTS_EVENTS: return settings.getInt("LISTS_EVENTS", 1);
case SHAPES_FACES: return settings.getInt("SHAPES_FACES", 1);
case SHAPES_ABSTRACT: return settings.getInt("SHAPES_ABSTRACT", 1);
case CARDS_SPEED: return settings.getInt("CARDS_SPEED", 1);
default: return -1;
}
}
THE FOLLOWING CODE RESIDES IN A DIFFERENT ACTIVITY:
SharedPreferences settings = getSharedPreferences("Steps", 0);
step = settings.getInt("NUMBERS_SPOKEN", 1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 case 语句之间进行中断
,否则它将遍历每个 case 语句,这就是为什么它们都被分配了该值
You need breaks between your case statements
Otherwise it will go through every case statement which is why they are all getting assigned that value