简体   繁体   English

SharedPreferences不增加整数值-Android

[英]SharedPreferences not incrementing integer value - Android

I am wanting to store an int in SharedPreferences . 我想将一个int存储在SharedPreferences The int represents which level the user is on in my game. int代表用户在我的游戏中处于哪个级别。 Before the game starts, this code is executed: 在游戏开始之前,将执行以下代码:

    int level;

    // Restore preferences
    SharedPreferences settings = getSharedPreferences("level_SP", 0);
    level = settings.getInt("currentLevel", 0);
    if(level == 0) {
        level++;
    }
    String str = Integer.toString(level);
    title.setText(str); 

The text that is outputted from title.setText(str); title.setText(str);输出的文本title.setText(str); is 1 . 1

For testing purposes, no matter how the user does in the game he passes the level. 出于测试目的,无论用户在游戏中的表现如何,他都会通过关卡。 After the game is over, this code is executed. 游戏结束后,将执行此代码。

public void levels() {

    // do calculations here to see if user passed level.


    // Restore preferences
    SharedPreferences settings = getSharedPreferences("level_SP", 0);
    int level = settings.getInt("currentLevel", 0);
    int newLevel = level++;

    // We need an Editor object to make preference changes.
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("currentLevel", newLevel);

    // Commit the edits!
    editor.commit();
}

Then when the user starts the game again, they should be at level 2. In the code pasted at the top of this post, this line of code: title.setText(str); 然后,当用户再次开始游戏时,他们应该处于级别2。在此文章顶部粘贴的代码中,以下代码行: title.setText(str); should output 2 , but it still is saying 1 . 应该输出2 ,但是仍然是1 That tells me that the int I stored in SharedPreferences is not incrementing. 这说明我存储在SharedPreferences中的int没有增加。

My question is, am I "getting" the SharedPreferences correctly? 我的问题是,我是否正确地“获取”了SharedPreferences If yes, am I "editting" the SharedPreferences correctly? 如果是,我是否正确“编辑”了SharedPreferences If not on either one, what am I doing wrong? 如果没有一个,我在做什么错?

EDIT 编辑

Thought: Do the 0 that are scattered throughout my above code mean anything? 想法:分散在我上面的代码中的0表示什么? Should they be something else? 他们应该是别的东西吗?

Use 采用

editor.putInt("currentLevel", newLevel);

instead. 代替。

When you get the new level you're writing it to a different preference than when you read. 当您获得新级别时,您将以与阅读时不同的偏好来编写它。

It looks like you want to change this 您似乎要更改此设置

editor.putInt("newLevel", newLevel);

to

editor.putInt("currentLevel", newLevel);

You are creating a new SharedPreference instead of editing the exisiting so the currentLevel never changes from 1 您正在创建新的SharedPreference而不是编辑现有的,因此currentLevel永远不会从1更改

This line is the problem: 这行是问题所在:

int newLevel = level++;

you are using the post-increment operator, which means that this is the same as: 您正在使用后递增运算符,这意味着它与:

int newLevel = level;
level = level + 1;

try 尝试

int newLevel = level + 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM