Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of SharedPreferences.getFloat()

The method getFloat() exists to pull a float value from android's SharedPreferences API. Yet, in the xml, EditTextPreference always stores a string value, even if numeric is defined.

One would expect getFloat() to automatically return this but instead it throws a ClassCastException and we have to use Float.parseFloat(SharedPreferences.getString()) to get this value.

Is there literally no use for getFloat() or am I missing something here?

like image 886
Molten Ice Avatar asked Jun 22 '14 21:06

Molten Ice


1 Answers

The method getFloat() exists to pull a float value from android's SharedPreferences API.

Yes.

Yet, in the xml, EditTextPreference always stores a string value, even if numeric is defined.

I think you are mixing up things here. You are setting SharedPreferences equal to elements on a PreferenceScreen.

PreferenceScreen's save all their values into your SharedPreferences and it's true that EditTextPreferences can't save a float, but you can however save a float into your SharedPreferences yourself:

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("aFloat", 1.2f);
editor.commit();

and then retrieve the float like this:

float someFloat = prefs.getFloat("aFloat", 0.0f);
like image 177
Ahmad Avatar answered Oct 31 '22 15:10

Ahmad