Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video I implement SharedPreferences for the application.
-
0:00
To fix that I'm going to use shared preferences.
-
0:03
I'm going to start by creating a shared preferences field and an editor field.
-
0:09
So private shared preferences mshared preferences and
-
0:13
then private sharedpreferences.editor meditor.
-
0:21
Now that I have these two fields I'm going
-
0:23
to initialize them right below setContentView.
-
0:25
So mSharedPreferences = getSharedPreferences.
-
0:31
And we'll need a preferences file name.
-
0:33
And then Context.MODE_PRIVATE.
-
0:41
Alt+Enter to create this constant, and
-
0:45
I'll call it my package name plus .preferences.
-
0:49
So com.teamtreehouse.golfscorecard.preferen-
-
0:56
ces.
-
0:59
Looks good.
-
1:01
Then I'll initialize the editor to our mSharedPreferences.edit.
-
1:09
Great.
-
1:09
Now we've got access to all the pieces we need and I can start saving the values.
-
1:15
I'll do that in the on pause method.
-
1:19
Make some space at the bottom and use control o to override the on pause method.
-
1:27
And the on pause method, I will want to save the score for
-
1:31
each whole end to a different shared preference.
-
1:35
Here is how I will do it.
-
1:38
First we need to loop through all the holes.
-
1:40
So for int I, equals zero, I less than M
-
1:44
holes.length, I plus plus.
-
1:51
We'll use the editor, so M editor.put.int.
-
1:56
Because we're storing an Int in our shared preference.
-
2:00
And we'll use a key of KEY_STROKECOUNT, which I'll create now.
-
2:10
Alt+Enter, key_strokecount.
-
2:15
Looks good.
-
2:19
And then I'm going to add to this key our index, I.
-
2:29
This is going to make 18 different keys for
-
2:33
our shared preferences, and is going to let us store 18 different values.
-
2:39
Then we need to put the value into that key which will be mHoles,
-
2:47
the holes at that index.get stroke count.
-
2:53
Perfect.
-
2:54
Then after the four loop and we've put all of our strokes into the appropriate key.
-
3:01
We need to call apply to set those changes.
-
3:05
Looks good.
-
3:06
Now that we've saved our value in onPause,
-
3:10
I'm going to retrieve them in onCreate and set them.
-
3:15
So for each hole, we're going to set strokes
-
3:21
equal to our SharedPreferences object.getInt, and we'll pass in our key,
-
3:27
which is KEY_STROKECOUNT plus the index,
-
3:33
and then we'll pass in a default value of 0.
-
3:37
And this will set our strokes for each hole.
-
3:43
All right.
-
3:44
Looks good.
-
3:45
Let's see what happens when we run it.
-
3:49
Now I'm going to put in some values 3, 2, 1, 1.
-
3:52
Maybe scroll to the bottom.
-
3:57
Put some in here.
-
4:00
Give it 10 for hole 15, that'll be easy to check, and
-
4:03
then I'll hit the back button to completely destroy the activity.
-
4:09
Let's go back into golf scorecard, and it looks like it saved our values.
-
4:15
Even the 10 we put for hole 15.
-
4:17
Great, it looks like our shared preferences are working.
You need to sign up for Treehouse in order to download course files.
Sign up