Matt
Matt

Reputation: 3912

Offline highscores with SharedPreferences - Android (java)

I think I have a fully functional trivia game right now. At the end of the game on the results page it shows a score(long) and percentage(int). I want to implement an offline SharePreferences highscores now where 3 variables are inputted -- rank(int), score(long) and percentage(int). I was trying to first just implement a highscore with one of the variables and then add the other 2 later but I seem to be failing badly. You can see in my activity below a lot of commented out code where I was trying stuff and a lot of other attempts have already been deleted.

This is my very first time trying to use SharedPreferences so I don't have a solid grasp on it yet.

Results.java

public class Results extends Activity {

    public static final String SP_NAME = "TEST";
    public static final String INT_PERCENTAGE = "SP_percentage";
    public static final String INT_RANK = "SP_rank";
    public static final String LONG_SCORE = "SP_score";
    private SharedPreferences mPrefs;

    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

    int cAnswers, wAnswers, percentage, rank;

    long score;

    ArrayList<Question> qs;

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

        mPrefs = getSharedPreferences(SP_NAME, 0);

        cAnswers = getIntent().getIntExtra("correctAnswers", -1);
        wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
        score = getIntent().getLongExtra("score", -1);

        qs = getIntent().getParcelableArrayListExtra("queries");

        Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn);
        mainmenuBtn.setText("Main Menu");

        mainmenuBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                restart();
            }
        }); 

        //Retrieving high score
        SharedPreferences mPrefs = getSharedPreferences(LONG_SCORE, 0);
        SharePreferences.Editor editor = prefs.edit();
        long highscore = mPrefs.getLong(LONG_SCORE, 0);

        //Saving current score as high score
/*      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInteger(HIGH_SCORE, currentScore);*/
        // Commit the edits!
        editor.commit();

        SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
/*      SharedPreferences.Editor editor = prefs.edit();  
        return editor.commit();*/

        showResults();
    }

    //...other code
}

Upvotes: 0

Views: 661

Answers (2)

impact
impact

Reputation: 396

It looks like your question got pretty well answered, but I'm just gonna throw this out there for perspective (the more you know! right?).

You may find it easier to use an online leaderboard system instead of dealing with maintaining one locally. It can save you a good bit of work and give you the benefit that players can then easily compete globally. Some options to check into are Swarm (aka SwarmConnect), Scoreloop or Papaya. All 3 of them offer a way to get global leaderboards up and running, although I think Swarm's system is probably the fastest and easiest to setup out of the box.

Cheers!

Upvotes: 1

Neto Marin
Neto Marin

Reputation: 674

The point is, you didn't understand the usage of a SharedPreference. To this high score stuff, you should use a SQLite database or a text file.

Please, read the guide http://developer.android.com/guide/topics/data/data-storage.html to a better understanding of different types of persistence methods in Android.

BTW, you should create a table with the three columns you said (rank(int), score(long) and percentage(int)) and do a little CRUD to have your high score working well. You will have to understand how to use the SQLiteOpenHelper and create your own, to do the table creations procedures. And then, create you model class to manage the data.

So, search in Google for SQLite tutorials if you still having doubts after reading the developer.android.com guide.

Upvotes: 2

Related Questions