【问题标题】:How to pass data between activities on Android with SharedPreferences?如何使用 SharedPreferences 在 Android 上的活动之间传递数据?
【发布时间】:2016-10-01 22:41:58
【问题描述】:

我想将游戏分数从活动 1 (SummaryActivity.java) 保存并加载到活动 2 (LevelSelect.java)。 因此,当达到某个分数时,一些按钮将可见。 我尝试了这种方法,但不起作用。

如何为这些不同的活动实现 SharedPreferences?

SummaryActivity.java 是玩完游戏后的一个类。

我使用 int result 将数据从 SummaryActivity.java 传递到 LevelSelect.java 并且不起作用。

我想通过 SharedPreferences 将此分数发送到其他活动 (LevelSelect.java),以便我可以使用按钮制作一些关卡。

级别 2 到级别 10 的默认按钮是不可见的(消失),并且在得分达到 700 多分后,我想让这些按钮可见。所以玩家可以玩更高级别的游戏。

但是,当我将 result 更改为 800(带数字)时,此代码运行良好。我不知道为什么 result 不起作用。

那个结果没有保存分数。但是,如果我使用 Google 排行榜,那就可以了。

这是 SummaryActivity.java:

int result;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.summary);

    SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor=saved_values.edit();
         editor.putInt("level1", result);
         editor.apply();
mclient = getApiClient();

    if(mclient.isConnected())
       {
    Games.Leaderboards.submitScore(getApiClient(),
         getString(R.string.leaderboard_level_1),
         result); // this result is work

       }
}

private void displaySummary()
{
    String summary = getString(R.string.answered)
            + " " + mCorrectAnswers
            + " " + getString(R.string.of)
            + " " + mTotalQuestions
            + " " + getString(R.string.questions_correctly);

    mSummaryText.setText(summary);

    int score = (int)(((double)mCorrectAnswers / (double)mTotalQuestions) * 1000); // this is score

    result = (score);

    mScoreText.setText(score + ""); 
    }

这是 LevelSelect.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.levelselect);

    SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(this);
    count = saved_values.getInt("level1", 0);

    // This button is not visible by default.
    // I want to make it visible when result score reached 700 pts
    Button level2Button = (Button) findViewById(R.id.level2Button);
    level2Button.setOnClickListener (new View.OnClickListener() {
        public void onClick(View v) {
            Sound.playButtonSelect();
            Intent i = new Intent(getApplicationContext(),L2_MainActivity.class);
            startActivity(i);
            finish();
        }
    });

    if (count > 700)
    {
        level2Button.setVisibility(View.VISIBLE);
    }

这是给按钮的:

<Button
        android:id="@+id/level2Button"
        style="@style/FeedbackButton"
        android:layout_height="40dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="10dp"
        android:visibility="gone"
        android:text="@string/level2" />

已解决。这是我的新 SummaryActivity.java:

int result;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.summary);


mclient = getApiClient();

    if(mclient.isConnected())
       {
    Games.Leaderboards.submitScore(getApiClient(),
         getString(R.string.leaderboard_level_1),
         result); // this result is work

       }
}

private void displaySummary()
{
    String summary = getString(R.string.answered)
            + " " + mCorrectAnswers
            + " " + getString(R.string.of)
            + " " + mTotalQuestions
            + " " + getString(R.string.questions_correctly);

    mSummaryText.setText(summary);

    int score = (int)(((double)mCorrectAnswers / (double)mTotalQuestions) * 1000); // this is score

    result = (score);

    mScoreText.setText(score + ""); 

SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor=saved_values.edit();
         editor.putInt("level1", result);
         editor.apply();
    }

【问题讨论】:

    标签: java android sharedpreferences


    【解决方案1】:

    改变

    int myIntValue = sp.getInt("result", 0);
    

    int myIntValue = sp.getInt("level1", 0);
    

    【讨论】:

    • 嗨@uguboz,我尝试但不工作。当我达到 700 分时,按钮仍然消失。
    • 试试 Log.d(getClass().getSimpleName(), "result is "+ myIntValue);看看是不是真的
    • 而你的 if 条件是 >700 而不是 >=700
    • 是的,我的条件是 > 700。我正在更新我的问题。
    • 首先为什么在结果为空时使用sharedpref编辑器?每次设置结果后,您都应该使用 sharedpref 编辑器。因此 sharedpref 可以使用最新的结果值更新其值。
    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多