-2

I have a problem when it comes to create a high score view. The point of the game is to click as many times as you can on a button in 15 seconds and I would like to make appear a high score in the right corner for instance. Here is my code:

public class newgame extends Activity {
    int clicks = 0;
    TextView textCount;
    Button buttonCount;
    int guessCount =0;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newgame);
    final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);

    final TextView textView = (TextView) findViewById(R.id.applesEaten);
    buttonCount = (Button) findViewById(R.id.button);

    buttonCount.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            clicks++;
            textView.setText("Clicks: " + clicks);
            TextView textView = (TextView)findViewById(R.id.topScoreView);
                    textView.setText("Best: " + oldscore);
        }
    });
    final TextView textic = (TextView) findViewById(R.id.textView2);
    CountDownTimer Count = new CountDownTimer(15000, 1000) {
        public void onTick(long millisUntilFinished) {
            int seconds = (int) ((millisUntilFinished / 1000));
            textic.setText("Time Left: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            textic.setText("Time's Up!");
            buttonCount.setEnabled(false);
            if (clicks > oldscore)
                getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
        }
    };
    Count.start();
}

How would I go proceed to achieve this?

Shizzle
  • 53
  • 1
  • 13
  • where is shared pref code above? – KOTIOS Aug 15 '14 at 13:10
  • 3
    possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – EpicPandaForce Aug 15 '14 at 13:12

3 Answers3

0

Zhuinden's comment is pretty much what you want. But I'll try to suggest something that might be just fit straight in - although I would suggest you experiment with these things yourself a bit

Add the following variables:

int topScore = 0;
SharedPreferences prefs;

Add this to your onCreate method

prefs = _context.getSharedPreferences(
            "com.example.app", Context.MODE_PRIVATE);

TextView topScore = (TextView) findViewById(R.id.topScoreView);

topScore.setText(prefs.getInt("topScore", 0);

Change your onFinish to the following:

    public void onFinish() {
        textic.setText("Time's Up!");
        buttonCount.setEnabled(false);
        if (guessCount > topScore) {
            prefs.edit().putInt("topScore", guessCount).apply();
        }
    }

This should work I think but haven't actually tested it

Dreagen
  • 1,733
  • 16
  • 21
0

Add a value to sharedPreferences

getSharedPreferences("myPrefs", MODE_PRIVATE).putInt("highscore", clicks).commit();

Retrieve a value frome sharedPreferences

getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);

implemented:

public class newgame extends Activity {
    int clicks = 0;
    TextView textCount;
    Button buttonCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newgame);
        final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);

        final TextView textView = (TextView) findViewById(R.id.textView);
        buttonCount = (Button) findViewById(R.id.button);

        buttonCount.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                clicks++;
                textView.setText("Clicks: " + clicks + "(old highscore = " + oldscore + ")");
            }
        });
        final TextView textic = (TextView) findViewById(R.id.textView2);
        CountDownTimer Count = new CountDownTimer(15000, 1000) {
            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished / 1000));
                textic.setText("Time Left: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                textic.setText("Time's Up!");
                buttonCount.setEnabled(false);
                if (clicks > oldscore)
                    getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
            }
        };
        Count.start();
    }
}

Your code can do with some tweaking though

stealthjong
  • 10,858
  • 13
  • 45
  • 84
0

Okay, in order to use shared preferences to store the score value you have stored in the guessCount variable, you'll first need to get the shared preferences for your application. To do this, you can use:

SharedPreferences sharedPrefs = this.getSharedPreferences(
"com.example.yourAppName", Context.MODE_PRIVATE);

Once this is done, you can add the score value into the preferences by using:

sharedPrefs.putInt("High Score", guessCount).apply();

Then later when you want to retrieve the high score for use, you'll need to make sure you still have a SharedPreferences object in the manner I described above, but to read data from it you just use:

int retrieveCount = sharedPrefs.getInt("High Score", 0)

The value in retrieveCount will be whatever you stored earlier. Be careful to make sure that there is a value stored in SharedPreferences or you will get back 0.

Additionally: Android Documentation for SharedPreference

JGagnon
  • 55
  • 1
  • 12