PreferenceActivity: storing value as a whole

Using a simple EditTextPreference in my activity preferences:

 <EditTextPreference android:key="SomeKey" android:title="@string/some_title" android:summary="..." android:numeric="integer" android:maxLength="2" /> 

Can this configuration value be saved as an integer? It seems now it just lets you enter numbers, but the value is saved as a string:

Vocation:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); int value = preferences.getInt("SomeKey", -1); 

throws me java.lang.ClassCastException: java.lang.String and:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String value = preferences.getString("SomeKey", "-1"); 

retrieves the value successfully.

How to make PreferenceActivity to save value as integer by default?

+49
android sharedpreferences preferenceactivity
Sep 15 2018-10-15T00:
source share
4 answers

You can extend EditTextPreference:

 public class IntEditTextPreference extends EditTextPreference { public IntEditTextPreference(Context context) { super(context); } public IntEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); } public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected String getPersistedString(String defaultReturnValue) { return String.valueOf(getPersistedInt(-1)); } @Override protected boolean persistString(String value) { return persistInt(Integer.valueOf(value)); } } 

It would be better to rewrite the onSetInitialValue () and setText () methods, but then you have to copy some code from the base class. The above solution is simpler, but it's rather complicated - the "string" methods do something with ints. Try not to extend this class further; -)

You can use it from XML:

 <package.name.IntEditTextPreference android:key="SomeKey" android:title="@string/some_title" android:summary="..." android:numeric="integer" android:maxLength="2" /> 
+58
Sep 20 '10 at 21:22
source share

Even if you set android: numeric = "integer", it will be a text preference - as the name suggests. You can easily convert the string value to int using Integer.valueOf (). You can also overwrite PreferenceActivity to automatically convert on exit.




I think the best solution is to write a simple method to get this value from the settings. Something like:

 public static int getSomePref(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String value = prefs.getString("SomeKey", null); return value == null ? -1 : Integer.valueOf(value); } 

Then you can very easily use it from your code.

+7
Sep 15 '10 at 22:16
source share

Although the answer is accepted in the parking lot, I would like to share another shorter way to achieve this:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); int value = Integer.parseInt(preferences.getString("SomeKey", "-1")); 

Since you have already established that only numbers can be entered, this will not happen with any exception. but to complete my answer:

 <EditTextPref android:key="SomeKey" android:title="@string/some_title" android:summary="..." android:numeric="integer" android:maxLength="2" /> 
+3
Jan 18 '16 at 10:25
source share

I had the same problem. (I wanted SharedPreference to give me the port number that I saved in the XML preferences file as defaultValue).

Implementing all of the SharedPreferences methods will be of great importance, so it would be best to think about writing a custom method in the class that created the SharedPreferences, as suggested by broot .

You can also use the Static Integer method in the line where you need it:

 int number = Integer.valueOf(settings.getString("myNumberString", "0")); 
0
Jan 18 '16 at 9:58
source share



All Articles