0

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;
        }
    }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
yep
  • 1
  • 1
    You are storing the high score in the SharedPreferences called "mejorScore" and then trying to load it from the SharedPreferences called "bestScore". That looks like a problem – Borre Mosch Nov 04 '14 at 17:25
  • @Borre just changed it, still getting the same problem, no save – yep Nov 04 '14 at 18:54

1 Answers1

3

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.

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • just changed it, still getting the same problem, no save – yep Nov 04 '14 at 18:52
  • @yep cleat your project and retry.see [View an Android App's shared preferences?](http://stackoverflow.com/questions/11354904/view-an-android-apps-shared-preferences) to view your shared preference file – Giru Bhai Nov 05 '14 at 04:44