获取错误,对于这种类型的sharedPreferences编辑器,未定义方法put double.Eclipse给编辑器一个快速修复添加强制转换,但是当我这样做时它仍然给出错误,为什么我不能放双。
代码:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (TextUtils.isEmpty(editBl.getText().toString())) {
numberOfBl = 0;
} else {
numberOfBl = Integer.parseInt(editBl.getText().toString();
}
if (TextUtils.isEmpty(editSt.getText().toString())) {
tonOfSt = 0;
} else {
tonOfSt = Double.parseDouble(editSt.getText().toString());
}
SharedPreferences prefs = getSharedPreferences(
"SavedTotals", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("savedBl", numberOfBl);
editor.putDouble("savedSt", tonOfSt);
editor.commit();
}
答案 0 :(得分:297)
那些建议使用 putFloat 和 getFloat 的人很遗憾是非常错误的。将double转换为float可导致
那些暗示 toString 和 parseString 的人没有错,但这是一个效率低下的解决方案。
处理这个问题的正确方法是将double转换为其'raw long bits'等效值并存储该long。当您读取该值时,请转换回double。
因为两种数据类型具有相同的大小,所以不会丢失精度,也不会导致{over,under}流。
Editor putDouble(final Editor edit, final String key, final double value) {
return edit.putLong(key, Double.doubleToRawLongBits(value));
}
double getDouble(final SharedPreferences prefs, final String key, final double defaultValue) {
return Double.longBitsToDouble(prefs.getLong(key, Double.doubleToLongBits(defaultValue)));
}
或者你可以把getter写成:
double getDouble(final SharedPreferences prefs, final String key, final double defaultValue) {
if ( !prefs.contains(key))
return defaultValue;
return Double.longBitsToDouble(prefs.getLong(key, 0));
}
答案 1 :(得分:14)
Kotlin扩展方式(比使用奇怪的utils类或其他任何东西更漂亮)
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)))
答案 2 :(得分:12)
我所做的是将首选项保存为字符串:
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString("double", "0.01").commit();
然后要检索double,只需使用Double.parseDouble:
Double.parseDouble(getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("double", "0.01"));
答案 3 :(得分:9)
你总是可以实现SharedPreferences并包装android实现。
package com.company.sharedpreferences;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.Map;
import java.util.Set;
public class EnhancedSharedPreferences implements SharedPreferences {
public static class NameSpaces {
public static String MY_FUN_NAMESPACE = "MyFunNameSpacePrefs";
}
public static EnhancedSharedPreferences getPreferences(String prefsName) {
return new EnhancedSharedPreferences(SomeSingleton.getInstance().getApplicationContext().getSharedPreferences(prefsName, Context.MODE_PRIVATE));
}
private SharedPreferences _sharedPreferences;
public EnhancedSharedPreferences(SharedPreferences sharedPreferences) {
_sharedPreferences = sharedPreferences;
}
//region Overrides
@Override
public Map<String, ?> getAll() {
return _sharedPreferences.getAll();
}
@Override
public String getString(String key, String defValue) {
return _sharedPreferences.getString(key, defValue);
}
@Override
public Set<String> getStringSet(String key, Set<String> defValues) {
return _sharedPreferences.getStringSet(key, defValues);
}
@Override
public int getInt(String key, int defValue) {
return _sharedPreferences.getInt(key, defValue);
}
@Override
public long getLong(String key, long defValue) {
return _sharedPreferences.getLong(key, defValue);
}
@Override
public float getFloat(String key, float defValue) {
return _sharedPreferences.getFloat(key, defValue);
}
@Override
public boolean getBoolean(String key, boolean defValue) {
return _sharedPreferences.getBoolean(key, defValue);
}
@Override
public boolean contains(String key) {
return _sharedPreferences.contains(key);
}
@Override
public Editor edit() {
return new Editor(_sharedPreferences.edit());
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
_sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
_sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
}
//endregion
//region Extension
public Double getDouble(String key, Double defValue) {
return Double.longBitsToDouble(_sharedPreferences.getLong(key, Double.doubleToRawLongBits(defValue)));
}
//endregion
public static class Editor implements SharedPreferences.Editor {
private SharedPreferences.Editor _editor;
public Editor(SharedPreferences.Editor editor) {
_editor = editor;
}
private Editor ReturnEditor(SharedPreferences.Editor editor) {
if(editor instanceof Editor)
return (Editor)editor;
return new Editor(editor);
}
//region Overrides
@Override
public Editor putString(String key, String value) {
return ReturnEditor(_editor.putString(key, value));
}
@Override
public Editor putStringSet(String key, Set<String> values) {
return ReturnEditor(_editor.putStringSet(key, values));
}
@Override
public Editor putInt(String key, int value) {
return ReturnEditor(_editor.putInt(key, value));
}
@Override
public Editor putLong(String key, long value) {
return ReturnEditor(_editor.putLong(key, value));
}
@Override
public Editor putFloat(String key, float value) {
return ReturnEditor(_editor.putFloat(key, value));
}
@Override
public Editor putBoolean(String key, boolean value) {
return ReturnEditor(_editor.putBoolean(key, value));
}
@Override
public Editor remove(String key) {
return ReturnEditor(_editor.remove(key));
}
@Override
public Editor clear() {
return ReturnEditor(_editor.clear());
}
@Override
public boolean commit() {
return _editor.commit();
}
@Override
public void apply() {
_editor.apply();
}
//endregion
//region Extensions
public Editor putDouble(String key, double value) {
return new Editor(_editor.putLong(key, Double.doubleToRawLongBits(value)));
}
//endregion
}
}
答案 4 :(得分:0)
检查这个要点https://gist.github.com/john1jan/b8cb536ca51a0b2aa1da4e81566869c4
我创建了一个Preference Utils类来处理所有情况。
易于使用
存储到首选项
PrefUtils.saveToPrefs(getActivity(), PrefKeys.USER_INCOME, income);
从偏好中获取
Double income = (Double) PrefUtils.getFromPrefs(getActivity(), PrefKeys.USER_INCOME, new Double(10));
答案 5 :(得分:0)
在 Kotlin 中,您可以将指定浮点值的位表示形式的双精度值转换为 Long,然后使用 putLong() 方法将该位表示形式保存在共享首选项中。 当您想从共享首选项中获取该值时,只需使用 Double.fromBits() 方法将其转换为 Double 值,您将获得一个没有精度损失的 double 值。 您可以阅读更多相关信息here。
以下是我在共享首选项中使用纬度和经度作为双精度值的示例:
val lat: Double = 40.23244412709637
val lng: Double = 14.280891281901097
private fun saveFetchedLocation(lat: Double, lng: Double) {
val sharedPreference: SharedPreferences = requireActivity().getSharedPreferences(
SHARED_PREF,
Context.MODE_PRIVATE
)
val editor: SharedPreferences.Editor = sharedPreference.edit()
editor.apply {
putLong("lat_value", lat.toBits())
putLong("lng_val", lng.toBits())
Log.i("Tag", "LONG_BITS: "+ " Lat: " + lat.toBits() + " Lng: " + lng.toBits())
}.apply()
}
private fun loadPreferences() {
val sharedPreference: SharedPreferences = requireActivity().getSharedPreferences(
SHARED_PREF,
Context.MODE_PRIVATE
)
val lat_double: Double = Double.fromBits(sharedPreference.getLong("lat_value", 1))
val lng_double: Double = Double.fromBits(sharedPreference.getLong("lng_val", 1))
Log.i("Tag", "FINAL_DOUBLE: $lat_double and $lng_double")
}
Logcat 结果:
//bit representation of the specified floating-point values as Long
I/Tag: LONG_BITS: Lat: 4630859030446343002 Lng: 4624229045136719443
//converted double values with no precision loss
I/Tag: FINAL_DOUBLE: 40.23244412709637 and 14.280891281901097