Issue
Some code autogenerated in a SettingsActivity looks like this (abbreviated version):
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference.getKey().equals("notification_period")){
preference.setSummary(stringValue + /* Resource: R.string.notification_period_summary*/));
}
return true;
}
};
I need to get the resource from strings.xml, but I don't think I have a context to use. I tried changing it to SettingsActivity.this.getString(R...)
, but it said I couldn't do that from a static method. I tried adding a Context context
to the method, but then it doesn't follow the signature it needs to override.
I'm not really sure where the method is being called from either, or if it is necessary for the signature to match for it to be called. There's another static method private static void bindPeferenceSummaryToValue
Which contains this line of code:
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
and I'm unsure what exactly that does either.
Solution
You can get the Context
with the method getContext()
of Preference
:
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference.getKey().equals("notification_period")){
String notificationPeriodSummary = preference.getContext().getResources().getString(R.string.notification_period_summary);
preference.setSummary(stringValue + notificationPeriodSummary));
}
return true;
}
};
Answered By - Mattia Maestrini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.