SharedPreferences无法正确存储

时间:2013-04-25 16:13:15

标签: android sharedpreferences

我有一个问题,我正在进行多次计算,每次结果,一旦最终计算完成,我就想将答案存储在共享首选项中,问题是我一直都是零。< / p>

这是我进行计算的循环代码和共享首选项

ACTIVITY 1

for (i = 0; i < happyRating.size()-1; i++) {
    int test = happyRating.get(i);


        if (happyRating.get(i) < happyRating.size()) {
            Log.d("TestTrain", "CALLED");
            int x, x1, x2, y, y1, y2;
            double learningRate = -0.00002;
            //double learningRate = -0.00092;

            x1 = happyRating.get(i);
            x2 = happyRating.get(i + 1);
            y1 = iteration[i];
            y2 = iteration[i + 1];

            x = x2 - x1;
            y = y2 - y1;

            if (x == 0) {
                slope = 0;
            } else {
                slope = (y2 - y1) / (x2 - x1);
            }

            double weightAdj = happyRating.get(i) * slope * learningRate;

            weighting = (weighting + weightAdj);
            dynA =  distArray[i] * weighting;

            Log.d("TestInt", "HappyRating (i): " + Integer.toString(test));
            Log.d("WEIGHTING", Double.toString(weighting)); 

            String PREFS1 = "siclPrefs1";
            SharedPreferences siclPrefs1 = getSharedPreferences(PREFS1,0);
            Editor editor = siclPrefs1.edit();
            editor.putFloat("Weight7", (float) weighting);
            editor.commit();

            if (dynA > 1)
            {
                break;
            }

        } else {
            break;
        }

    }

我正在使用sharedpreferences来存储一个布尔值,当我在下一个活动中检查它时没问题。如果有任何冲突,这里是

的代码
TrainingDone = true;

    String PREFS = "siclPrefs";
    SharedPreferences siclPrefs = getSharedPreferences(PREFS,0);
    Editor editor = siclPrefs.edit();
    editor.putBoolean("Train7", TrainingDone);
    editor.commit();

ACTIVITY 2

其他活动的提取如下:

       String PREFS = "siclPrefs";
    SharedPreferences siclPrefs = getSharedPreferences(PREFS, 0);

    String PREFS1 = "siclPrefs1";
    SharedPreferences siclPrefs1 = getSharedPreferences(PREFS1, 0);

            double weight = (double) siclPrefs1.getFloat("Weight7", 0);
            Boolean train = siclPrefs.getBoolean("Train7", false);

布尔值被提取得很好,但是权重显示为零,我明白这是因为它不在那里,默认值为零。我可以重复使用编辑器,还是我跌倒的地方?

此致 加里

2 个答案:

答案 0 :(得分:0)

您可以使用此PublicSharePreferences类。

public class PublicSharedPreferences {

    public static void setDefaults(String key, String value, Context context) {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      SharedPreferences.Editor editor = prefs.edit();
      editor.putString(key, value);
      editor.commit();
    }

    public static String getDefaults(String key, Context context) {
      SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(context);
      return preferences.getString(key, null);
    }

}

希望这有帮助。

答案 1 :(得分:0)

要获得偏好,请使用以下内容:

SharedPreferences prefs = this.getSharedPreferences(
                  "Package_Name", Context.MODE_PRIVATE);

要将数据保存到该首选项,请执行以下操作:

prefs.edit().putString("Key", "Value").commit();

从该首选项获取值执行以下操作:

prefs.getString("Key", null)