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 trialMalcolm Mutambanengwe
4,205 Pointsshared preferences
Hi Steve Hunter. Am now on the android activity lifecycle. Still much more to learn.
import android.view.View;
import android.os.Bundle;
public class MainActivity extends Activity {
public static final String PREFS_FILE;
public static final String KEY_CHECKBOX;
public SharedPreferences mSharedPreferences;
public SharedPreferences.Editor mEditor;
public CheckBox mCheckBox;
public EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
mEditText = mSharedPreferences.edit();
}
}
Malcolm Mutambanengwe
4,205 PointsKikiki. Yep. In the video PREFS_FILE and KEY_CHECKBOX both have values but not in the code given in the problem.
public static final String PREFS_FILE = preferences; public static final String KEY_CHECKBOX = key_edittext;
I tried assigning them values but that didn't work. Let me get the errors which I got.
Then initialize both of these fields in the onCreate method.
mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
mEditText = mSharedPreferences.edit()
4 Answers
Steve Hunter
57,712 PointsTry:
private static final String PREFS_FILE = "prefs";
private static final String KEY_CHECKBOX = "key_checkbox";
Steve Hunter
57,712 PointsActually, no. I've not done this course nor do I have any experience in database actions within Android; I don't know the solution.
Malcolm Mutambanengwe
4,205 PointsI'll wrestle with this one. Must have an answer by tomorrow afternoon.
Steve Hunter
57,712 PointsIs this a college course, or something? If I get a minute, I'll work through the video, if that'll help. Tomorrow's quite busy but I'll see if I can manage 20 minutes. And what time zone are you in? It's 21:30 here now.
Steve Hunter
57,712 PointsYes. When you assign a string to a variable, or constant, you need to treat it as a string, enclosing it with double quotes. So,
public String myName = "Steve Hunter";
public static final String MY_NAME = "Steve Hunter";
Yeah?
Steve.
Malcolm Mutambanengwe
4,205 PointsOk. So I can use quotes"" outside brackets as well. So in effect if a CONSTANT is a String I have to use quotes to assign it a value?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsOK - ask a question and then help me out. What did you expect your code to do; what did it do; what error did you get; why do you think that happened?
I'm not going to just chuck you a solution every time!
Steve.