0

Im in the process of learning some app building for android.

I wanna create an example of a high score system before trying to use it in a game.

Lets say that I have a timer running seconds and a stop button. If I press the stop button, the timer stops, and the number it has come to is saved (locally) to a highscore list. A top 5 for example.

How would I go about saving this highscore?

I've read a bit about SQLite and Shared preference, but I don't know what to use. Maybe there are even more options? I'm not looking for any online highscore list.

If you know of some good guides / tutorials, please link them to me.

  • Thanks.

2 Answers2

0

I use SharedPreferences for that kind of thing although it is a bit overly complex in my opinion. Once you've got it added to your manifest and figure out how to use it, however, it's fairly simply. It looks like the usage of SharedPreferences has been covered previously here: How to use SharedPreferences in Android to store, fetch and edit values. You can, of course, just save to a file as well which might be easier, but potentially easier to hack if you care. I don't know much about SQLight to make comments on that.

Community
  • 1
  • 1
CodeMonkey
  • 1,795
  • 3
  • 16
  • 46
0

Yes shared preferences would be suited for this. Here is a kick off example - but better use lists :

private static final String[] SCORES_KEYS = { "1", "2", "3", "4", "5" };
private static final int SCORES = 5;
//...
{
    SharedPreferences sp = getDefaultSharedPreferences(); // in an activity,
    // service or other context. DO NOT USE NAMED preferences
    final long timer = getYourTimerScore();
    int position = SCORES;
    long[] lowerScores = new long[SCORES + 1];
    for (int i = SCORES; i > 0;) {
        long j = sp.getLong(SCORES_KEYS[--i], 0);
        if (j < timer) {
            --position;
            lowerScores[i + 1] = j;
        } else break;
    }
    if (position < SCORES) {
        lowerScores[position] = timer;
        Editor ed = sp.edit();
        for (int i = position; i < SCORES; ++i) {
            ed.putLong(SCORES_KEYS[i], lowerScores[i]);
        }
        ed.commit();
    }
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361