我有一個問題,我做了多次計算,每次都有結果,一旦最終計算完成,我就想將結果存儲在共享偏好中,問題是我保留得到零。SharedPreferences沒有正確存儲
,這裏是我的循環的計算是由和共享偏好
活動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();
活動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);
布爾被排出的細做,然而重量顯示爲零,我知道這是因爲它不在那裏,默認值爲零。我可以重複使用編輯器嗎?還是我摔倒的地方?
問候, 加里
嘿,存儲SharedPref的代碼是在一個循環中。你確定你想要嗎?每次都會覆蓋相同的pref。你每次都能正確地獲得Weight7的價值嗎? – SKK 2013-04-25 16:23:01
嗨,是的,進入重量7的價值是重量,這是正確的,這是我想存儲和檢索的最終覆蓋..有什麼明顯的錯誤? – 2013-04-25 16:26:52
如果你只是想要最後一個,那麼你應該在循環之外編寫你的編輯器,只是爲了減少你無用的讀/寫你的偏好。 – StoneBird 2013-04-25 16:35:10