我有一个问题,我多次计算,每次都有结果,一旦完成最终计算,我想将答案存储在共享偏好中,问题是我一直为零。
这是我的循环代码,计算和共享偏好
活动一
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();
活动二
对其他活动的提取如下完成
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);
布尔值提取得很好,但是权重显示为零,我知道这是因为它不存在并且默认为零。我可以重复使用编辑器还是那是我跌倒的地方?
问候,加里