Search code examples
androidsavesaving-data

Save Basic Statistics data?


I'm making a game that requires some statistics to be saved to memory (games played, games won, etc). I've tried to use sharedpreferences but there seems to be poor documentation from what I can see, and I haven't been able to get it to work. Am I missing something?

try {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        SharedPreferences stats = getPreferences(MODE_PRIVATE);
        CharSequence gampjla = "Games played: " + stats;
        Toast toast = Toast.makeText(context, gampjla, duration);
        toast.show();

        gamesPlayed++;
        SharedPreferences.Editor editor = stats.edit();
        editor.putInt("gameplay", gamesPlayed).commit();
        CharSequence gampla = "Games played: " + gamesPlayed;
        Toast toayst = Toast.makeText(context, gampla, duration);
        toayst.show();

    }
    catch (Exception e) {
            Context context = getApplicationContext();
            CharSequence errormsg = "Unable to save!! " + e;
            int duration = Toast.LENGTH_LONG;

            Toast toast = Toast.makeText(context, errormsg, duration);
            toast.show();
    }

I've tried variations but nothing works! Thanks for any help!

EDIT: The random letters in some of the variable names is because I wanted a different variable name for testing and because I was getting frustrated so I couldn't be bothered at that point!


Solution

  • Solved! It was a dumb issue. I had written 'putInt' but used 'getString'.