Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkylan hill
3,125 PointsMeaningOfLife answer
I'm lost on this one.
int answer = Context.getSharedPreferences"<MeaningOfLife>",(mSharedPreferences, 0);
3 Answers
Ben Jakuben
Treehouse TeacherI apologize - I have this Code Challenge in the wrong spot in this stage. It's supposed to come after the video called Adding a Storage Preference. I'm moving them now. Watch that video first and hopefully it makes more sense. :)
kylan hill
3,125 PointsTy Ben.
On Challenge Task 4 of 4 in Adding a Storage Preference.
Why does the code have to look like this
SharedPreferences.Editor editableSharedPreferences = mSharedPreferences
.edit()
.putInt("MeaningOfLife",42);
editableSharedPreferences.commit();
was getting an error with just the .commit();
SharedPreferences.Editor editableSharedPreferences = mSharedPreferences
.edit()
.putInt("MeaningOfLife",42)
.commit();
Ben Jakuben
Treehouse TeacherLet me look into it...we may have mocked SharedPreferences incorrectly. We ended up mocking the class to allow us hooks inside to test the code challenge answers.
Ben Jakuben
Treehouse TeacherOkay, I remember now (after checking my code). The commit() method returns a boolean value. In this example we are trying to assign the value into a SharedPreferences.Editor variable, though, which is why we get a type mismatch. We need to be particularly careful about method chaining when we are assigning the result into a variable.
Gunjeet Hattar
14,483 PointsWhat you see is an example of method chaining. You can chain methods if they refer to the same object. Though not a hard and fast rule, its always better to chain methods when possible.
However there are exceptions. I had answered a smilier question in one of the forums you may check it here Forum Answer
Hope this explains
Evan Anger
Treehouse Guest TeacherJust to add on to this the academic name for this is known a fluent interface.. Its an attempt to make certain classes easier to use by making them more readable/writable. A number of android classes use this style, SharedPreferences.Editor being one of them and as Gunjeet mentioned in his other forum answer AlertDialog.Builder uses it as well.
The key to it is that every method on an object modifies the object and returns it as well. This allows the method chaining to occur.
Pretty useful!
kylan hill
3,125 Pointskylan hill
3,125 PointsTy I got Challenge 1 of 1
int answer = mSharedPreferences.getInt("MeaningOfLife", 1);