Android SharedPreferences 所有变量分配给相同的值

发布于 2024-12-25 13:03:55 字数 1923 浏览 1 评论 0原文

我正在创建一个包含多项活动的应用程序。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2025-01-01 13:03:55

您需要在 case 语句之间进行中断

switch (gameType) {
    case NUMBERS_SPEED:   editor.putInt("NUMBERS_SPEED",   step);
    break;
    case NUMBERS_BINARY:  editor.putInt("NUMBERS_BINARY",  step);
    break;

,否则它将遍历每个 case 语句,这就是为什么它们都被分配了该值

You need breaks between your case statements

switch (gameType) {
    case NUMBERS_SPEED:   editor.putInt("NUMBERS_SPEED",   step);
    break;
    case NUMBERS_BINARY:  editor.putInt("NUMBERS_BINARY",  step);
    break;

Otherwise it will go through every case statement which is why they are all getting assigned that value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文