-1

I am developing a game and I have written all the code, including the code that makes sure the highscore int is updated. Now I am wondering how I could save this int with SharedPreferences. I am an beginning programmer, so could you give me all the steps I would have to do?

The variable highscore is in the MainView and I came to the understanding that you would have to save something with shared preferences in the MainActivity.

  • 1
    Show us what you have done and where the error is... – Lal May 25 '14 at 16:00
  • Ps I have only one highscore which is drawn in the middle top of the screen – user3654043 May 25 '14 at 16:01
  • There is no error, I woul just like to know how I would do that, I don't understand what I have to do when I search it on the internet... – user3654043 May 25 '14 at 16:02
  • I seriously doubt you have made any search... this is what I got googling for "android sharedpreferences tutorial": http://www.tutorialspoint.com/android/android_shared_preferences.htm **it took me 2 seconds** – Phantômaxx May 25 '14 at 16:03
  • @SachinG I don't understand how I would do that with an integer variable – user3654043 May 25 '14 at 16:04
  • @user3654043: just replace `putLong` and `getLong` with `putInt` and `getInt`? – Phantômaxx May 25 '14 at 16:05
  • @DerGolem That sort of tutorials show me how I would save Textview, buttons, or something like that – user3654043 May 25 '14 at 16:05
  • You can't save a Button. You mean a Button's text. Read that tutorial **in full**, and read it **twice**. It's all there. – Phantômaxx May 25 '14 at 16:06
  • I have one more question @DerGolem, _public static final **String** Name = "nameKey";_ would you need to replace the String with Int or something like that? – user3654043 May 25 '14 at 16:10
  • NO. The key has a **name**, which is **always a String**. But it also has a **value**, which is, in your case, the highScore **int** to save (and to retrieve). – Phantômaxx May 25 '14 at 16:12
  • But how would I then get the value to be the highscore int,which is in my view class, in my activity class @DerGolem – user3654043 May 25 '14 at 16:17
  • @user184994 has a good answer (and you should accept it)... I think it says it all. – Phantômaxx May 25 '14 at 16:19

1 Answers1

2

I like to create a simple helper class to deal with the saving and loading of values. That way you can keep all of the keys on one place.

public class PreferencesHelper {

    private SharedPreferences prefs;

    private static final String FILE_NAME = "file_name";

    public static final String KEY_HIGH_SCORE = "high_score";

    public PreferencesHelper(Context context) {
        prefs = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    }


    /**
     * Save the specified value to the shared preferences
     * 
     * @param key
     *            The key of the value you wish to load
     * @param defValue
     *            The value to store
     */
    public void save(String key, int value) {
        prefs.edit().putInt(key, value).commit();
    }


    /**
     * Load the specified value from the shared preferences
     * 
     * @param key
     *            The key of the value you wish to load
     * @param defValue
     *            The default value that will be returned if nothing is found
     */
    public int loadInt(String key, int defValue) {
        return prefs.getInt(key, defValue);
    }

}

Then, in your activity you just write:

PreferencesHelper prefs = new PreferencesHelper(this);

// Save
prefs.save(PreferencesHelper.KEY_HIGH_SCORE, 25000);

//Load
prefs.loadInt(PreferencesHelper.KEY_HIGH_SCORE, 0);
user184994
  • 17,791
  • 1
  • 46
  • 52
  • when I write that last bit of code in my activity I get an error: _Syntax error on tokens, ConstructorHeaderName expected instead_ – user3654043 May 25 '14 at 17:21
  • and I still don't understand how this piece of code would recognize that it has to save the highscore variable _public int highscore_ – user3654043 May 25 '14 at 17:22