不能把双共享preferences能把、preferences

2023-09-12 22:45:44 作者:浪荡先森@

获取错误,把双倍的方法是不确定的这种类型的共享preferences的editor.Eclipse是给定的一个速战速决的附加强制转换为编辑器,但是当我这样做,它仍给予错误,为什么不能我把双。

在code:

  @覆盖
保护无效的onPause(){
    // TODO自动生成方法存根
    super.onPause();

    如果(TextUtils.isEmpty(editBl.getText()的toString())){
        numberOfBl = 0;
    } 其他 {
        numberOfBl =的Integer.parseInt(editBl.getText()的toString()。

    }
    如果(TextUtils.isEmpty(editSt.getText()的toString())){
        tonOfSt = 0;
    } 其他 {
        tonOfSt = Double.parseDouble(editSt.getText()的toString());

    }




    共享preferences preFS = getShared preferences(
            SavedTotals,Context.MODE_PRIVATE);

            共享preferences.Editor编辑器= prefs.edit();

            editor.putInt(savedBl,numberOfBl);
                       editor.putDouble(savedSt,tonOfSt);


            editor.commit();
}
 

解决方案

这些谁建议使用的 putFloat 和 getFloat 的是不幸的是非常错误的。铸造双成float会导致

忘记precision 溢出 下溢 死小猫

这些表明一个的的toString 和 parseString 的都没有错,但它是一个低效率的解决方案。

处理这个问题的正确方法是长转换的双重其原始长位'等价和存储。当你正在阅读的价值,转换回翻番。

由于这两个数据类型具有相同的大小,你不会失去precision,你会不会导致{上方,下方}流。

编辑putDouble(最后的编辑器进行编辑,最后串键,最后的双重价值){    返回edit.putLong(键,Double.doubleToRawLongBits(值)); } 双getDouble(最后一个共享preferences preFS,最后串键,最后的双重设置defaultValue){ 返回Double.longBitsToDouble(prefs.getLong(键,Double.doubleToLongBits(设置defaultValue))); } iphone6 plus怎么手动改变定位

另外,你可以写吸气剂为:

双getDouble(最后一个共享preferences preFS,最后串键,最后的双重设置defaultValue){ 如果(!prefs.contains(键))         返回设置defaultValue; 返回Double.longBitsToDouble(prefs.getLong(键,0)); }

Getting error, the method put double is undefined for this type of sharedPreferences editor.Eclipse is given one quick fix add cast to editor, but when i do that its still given errors, Why cant i put double.

The code:

@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();
}

解决方案

Those who suggested to use putFloat and getFloat are unfortunately very wrong. Casting a double to a float can result in

Lost precision Overflow Underflow Dead kittens

Those suggesting a toString and parseString are not wrong, but it's an inefficient solution.

The correct way of dealing with this is to convert the double to its 'raw long bits' equivalent and store that long. When you're reading the value, convert back to double.

Because the two data types have the same size you don't lose precision and you won't cause an {over,under}flow.

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)));
}

Alternatively you can write the getter as:

double getDouble(final SharedPreferences prefs, final String key, final double defaultValue) {
if ( !prefs.contains(key))
        return defaultValue;

return Double.longBitsToDouble(prefs.getLong(key, 0));
}