共享preferences不存储正确 [英] SharedPreferences not storing correctly

查看:139
本文介绍了共享preferences不存储正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我每次都做的计算多次,结果,一旦最终计算已经完成,那么我想存储在共享preferences答案,问题是我不断收到零。

I have a problem, I am doing a calculation multiple times, with the outcome each time, once the final calculation has been done, I then want to store the answer in shared preferences, the problem is I keep getting zero.

这是我的code为循环计算是由和共享preferences

here is my code for the loop the calculations are made and the shared preferences

活动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;
        }

    }

我使用的共享preferences存储一个布尔值,当我检查,在今后的活动,很好。如果有任何冲突,这里的code为

I am using sharedpreferences to store a boolean and when I check that in the next activity that's fine. In case there's any conflicts, here's the code for that

TrainingDone = true;

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

活动2

在另一活性的提取如下进行

The extraction on the other activity is done as follows

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

布尔正在提取正常,但是重量显示为零,我明白,这是因为它不存在,默认值是零。我可以重复使用的编辑器或者是,如果我倒下?

The boolean is being extracted fine, however the weight is showing as zero, I understand that this is because it's not there and the default is zero. Can I reuse the editor or is that where I'm falling down?

问候,
加里

推荐答案

您可以使用此PublicShare preferences类。

You can use this PublicSharePreferences class.

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

}

希望这有助于。

这篇关于共享preferences不存储正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆