Reputation: 930
This might be Android 101, but I'm getting used to the SDK now. Anyhow, I simply do not understand the error. I want to update some checkbox selections based on my shared preferences file and I'm using the following method:
private void updatePreferencesData() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
Boolean textData = prefs.getBoolean(R.string.Chri, false);
CheckBox cb1 = (CheckBox) findViewById(R.id.chkbxChristmas);
cb1.setChecked(textData);
}
Android Studio doesn't like my use of R.string.Chri in Boolean textData = prefs.getBoolean(R.string.Chri, false);
It states: "getBoolean(java.lang.String, Boolean) in SharedPreferences cannot be applied to (int, Boolean)"
In my strings.xml I have the value:
<string name="Chri">Christmas</string>
When I simply change the line to
Boolean textData = prefs.getBoolean("Christmas", false);
It works fine
How is it that the strings in strings.xml is being handled differently?
Thank you!
Upvotes: 0
Views: 900
Reputation: 857
R.string.Chri
is an int. Instead use getResources().getString(R.string.Chri)
to retrieve the string.
Upvotes: 2
Reputation: 471
You should use:
prefs.getBoolean(this.getResources().getString(R.string.Chri), false);
Upvotes: 2
Reputation: 1007322
Android Studio doesn't like my use of R.string.Chri in Boolean textData = prefs.getBoolean(R.string.Chri, false);
Correct. R.string.Chri
is an int
. To get a string, call getString(R.string.Chri)
on some Context
, such as your activity.
Upvotes: 3