I have set up my settings activity and when running my emulation I can edit my setting.. close the app and start it again and it stays the same all fine. Now when I want to reference this from an activity textview, I get the key I first wrote into strings.xml which is unitnumkey, yet the settings activity stores it fine..
What exactly am I missing here?
here is pref_general.xml
android:key="@string/pref_file_key_unit_number"
android:title="@string/pref_title_unit_number"
android:defaultValue="@string/pref_default_unit_number"
android:selectAllOnFocus="true"
android:inputType="phone"
android:singleLine="true"
android:maxLines="1" />
Strings.xml
<string name="pref_header_general">General</string>
<string name="pref_title_unit_number">Unit SMS Number</string>
<string name="pref_default_unit_number">0756745745</string>
<string name="pref_file_key_unit_number">unitnumKey</string>
textview activity
String phoneNum = getString(R.string.pref_file_key_unit_number);
final TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(phoneNum);
settingsactivity
public static class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
bindPreferenceSummaryToValue(findPreference("unitnumKey"));
}
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
Your not opening preferences at all, your just reading the String value out.
You need to do: UPDATE 2
String phoneNum = getString(R.string.pref_file_key_unit_number);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String myValue = preferences.getString(phoneNum, null);
final TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(myValue);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.