Can't Put Double Sharedpreferences

How to store double using SharedPreferences?

getSharedPreferences("MY_PREFERENCE", MODE_PRIVATE).edit().putString("double", "0.28").commit();

and then to retrieve the double, simply use Double.parseDouble:

Double.parseDouble(getSharedPreferences("MY_PREFERENCE", MODE_PRIVATE).getString("double", "0.28"));

How to return a double from SharedPreferences (Android)

defaultValue is what you get when there is no such key defined as "key" in shared pref, example if you are retrieving data for first time and you didn't write a value for your key it will return defaultValue.

double getDouble(final SharedPreferences prefs, final String key, final double defaultValue)
{ return Double.longBitsToDouble(prefs.getLong(key, Double.doubleToLongBits(defaultValue)));}

lets say you have put 542.3 into myDouble

getDouble("myDouble" , 5.0); // will return 542.3

but if you haven't put anything into myDouble

getDouble("myDouble" , 5.0); // will return 5

The common use, is when keep track of data, and your users can clear that data (imagine a highscore in a game) so you don't want to check if it exists, instead you launch it with default value.

getDouble("highscore" , 0.0); // will return highscore if exists otherwise will return 0

Unable to save and retreive Double from storage

You can use much elegant approach. https://stackoverflow.com/a/18098090/5422725 this is variant for java. But in case you use kotlin, you could use extension to make it mor eclear.

Kotlin extension way (much more pretty than using weird utils classes or whatever)

fun SharedPreferences.Editor.putDouble(key: String, double: Double) =
putLong(key, java.lang.Double.doubleToRawLongBits(double))

fun SharedPreferences.getDouble(key: String, default: Double) =
java.lang.Double.longBitsToDouble(getLong(key, java.lang.Double.doubleToRawLongBits(default)))

enter link description here

SharedPreferences same value

Your two keys have the same value (i.e "0"). Try to change one of them and your problem should be fixed. :)

How to use SharedPreferences in Android to store, fetch and edit values

To obtain shared preferences, use the following method
In your activity:

SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);

To read preferences:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());

To edit and save preferences

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();

The android sdk's sample directory contains an example of retrieving and storing shared preferences. Its located in the:

<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory

Edit==>

I noticed, it is important to write difference between commit() and apply() here as well.

commit() return true if value saved successfully otherwise false. It save values to SharedPreferences synchronously.

apply() was added in 2.3 and doesn't return any value either on success or failure. It saves values to SharedPreferences immediately but starts an asynchronous commit.
More detail is here.



Related Topics



Leave a reply



Submit