yep
yep

Reputation: 1

My Android application does not store the high score

I'm developing a simple game in Android and even i followed the steps found in the net, the highscore of my app is never saved. I'm using SharedPreferences to store, but I'm quietly sure the problem is in there, because I don't understand at all how to use it.Hope you guys can help me, thanks.

    package com.example.memory;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

public class Final extends Activity implements OnClickListener{
    TextView levelReachedText;
    TextView bestScoreText;
    int levelReached, bestScore;    
    Intent intent;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finals);
        levelReachedText = (TextView) this.findViewById(R.id.nivel);
        bestScoreText = (TextView) this.findViewById(R.id.best);
        Button menu = (Button) findViewById(R.id.inicio);
        menu.setOnClickListener(this);
        intent = getIntent();
        levelReached = intent.getIntExtra("nivel", 1);
        SharedPreferences preferences = this.getSharedPreferences("bestScore", MODE_PRIVATE);
        int savedScore = preferences.getInt("selectedScore", 0);

        levelReachedText.setText("You reached level "+levelReached );
        if(savedScore>levelReached){
            bestScore = savedScore;
        }else{
            bestScore = levelReached;
        }
        bestScoreText.setText("Maximum level reached "+levelReached);


}
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.inicio:
                SharedPreferences preferences = this.getSharedPreferences("mejorScore", MODE_PRIVATE);
                preferences.edit().putInt("selectedScore", bestScore).commit();
                this.finish();
                break;
        }
    }

}

Upvotes: 0

Views: 74

Answers (1)

Giru Bhai
Giru Bhai

Reputation: 14398

you are saving score value in mejorScore and trying to get it from bestScore.So either change

SharedPreferences preferences = this.getSharedPreferences("mejorScore", MODE_PRIVATE);

to

SharedPreferences preferences = this.getSharedPreferences("bestScore", MODE_PRIVATE);

or vice-versa.

Upvotes: 3

Related Questions