Data on main activity doesn't save/load properly with SharedPreferences應用程序不保存數據 - SharedPreferences
見上文從其他人的原始的問題和答案的鏈接......概括起來講,SharedPreferences或者不保存或不正確加載數據...看到我的應用程序的照片,這將更好地說明問題:由不同的人提出
Screenshots of my app in progress
至於主要活動背後的代碼,我曾嘗試不同的東西,並且我感謝任何曾經或願意幫助的人:)在在這個時候,這是與MainActivity中的問題相關的代碼:(請注意:爲了節省空間,我只顯示一個Bundle(getFromQuizOne),但是對於所有10個測驗,我都有相同類型的Bundle/Bundle代碼。此外,我只放入一個測驗按鈕setOnClickListener,但同樣的代碼適用於所有其他測驗按鈕)。
//Get points from Quiz One
Bundle getFromQuizOne = getIntent().getExtras();
if(getFromQuizOne != null)
{
//Get points from Quiz One
numberQuizOnePoints = getFromQuizOne.getInt("PointsFromAllQuizOne");
SharedPreferences shareInt = getApplicationContext().getSharedPreferences(QUIZ, MODE_PRIVATE);
SharedPreferences.Editor editInt = shareInt.edit();
//Save the value of points from Quiz One with SharedPreferences
editInt.putInt("quizOnePointsYeah", numberQuizOnePoints);
editInt.commit();
//Call arrayMethod, which uses for loop to get and display value of points for each quiz
arrayMethod();
}//end if
//My actual project has same kind of Bundle for all other quizzes, but to save space, i am just listing Bundle for quiz 1
//Directs user to Quiz One, first screen
quizOneButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Subtract out any points that user already got for Quiz 1 from total...
//In case user wants to or has to re-take Quiz 1
totalPoints = totalPoints - numberQuizOnePoints;
//Subtract out any points that user already got for Quiz 1 from itself, equal 0
numberQuizOnePoints -= numberQuizOnePoints;
//Go to the Quiz One, first screen
Intent directToQuizOnePartOne = new Intent();
directToQuizOnePartOne.setClass(getBaseContext(), QuizOnePartOne.class);
startActivity(directToQuizOnePartOne);
finish();
}//end void onClick quizOne
});//end setOnClickListener quizOne
//My project has button click listeners for all 10 quizzes, that look similar to quizOneButton onClick action listener, but to save on space, I didn't paste it here
public void arrayMethod()
{
int[] points = {numberQuizOnePoints, numberQuizTwoPoints, numberQuizThreePoints,
numberQuizFourPoints, numberQuizFivePoints, numberQuizSixPoints, numberQuizSevenPoints,
numberQuizEightPoints, numberQuizNinePoints, numberQuizTenPoints};
String[] prefKeys = {"quizOnePointsYeah", "quizTwoPointsYeah", "quizThreePointsYeah",
"quizFourPointsYeah", "quizFivePointsYeah", "quizSixPointsYeah", "quizSevenPointsYeah",
"quizEightPointsYeah", "quizNinePointsYeah", "quizTenPointsYeah"};
SharedPreferences shareInt = getApplicationContext().getSharedPreferences(QUIZ, MODE_PRIVATE);
for (int i = 0; i < 10; i++)
{
if (shareInt.getInt(prefKeys[i], 0) > 0)
{
points[i] = shareInt.getInt(prefKeys[i], 0);
}//end if
else
{
points[i] = 0;
}//end else
}//end for loop
//Sum up points from each quiz, and put sum in totalPoints variable
totalPoints = numberQuizOnePoints + numberQuizTwoPoints + numberQuizThreePoints
+ numberQuizFourPoints + numberQuizFivePoints + numberQuizSixPoints
+ numberQuizSevenPoints + numberQuizEightPoints + numberQuizNinePoints
+ numberQuizTenPoints;
quizOnePointsText.setText(String.format("%d", numberQuizOnePoints));
quizTwoPointsText.setText(String.format("%d", numberQuizTwoPoints));
quizThreePointsText.setText(String.format("%d", numberQuizThreePoints));
quizFourPointsText.setText(String.format("%d", numberQuizFourPoints));
quizFivePointsText.setText(String.format("%d", numberQuizFivePoints));
quizSixPointsText.setText(String.format("%d", numberQuizSixPoints));
quizSevenPointsText.setText(String.format("%d", numberQuizSevenPoints));
quizEightPointsText.setText(String.format("%d", numberQuizEightPoints));
quizNinePointsText.setText(String.format("%d", numberQuizNinePoints));
quizTenPointsText.setText(String.format("%d", numberQuizTenPoints));
actualPointsText.setText(String.format("%d", totalPoints));
}//end void arrayMethod
}//end class MainMenu
請不要在問題中發佈整個代碼。在大多數情況下,只有相關的代碼是足夠的。 –
這麼多重複的代碼。嘗試將這些代碼放在方法中,也許使用int數組來表示點等等。我們只需要你正在讀取和保存值的代碼。 – Shaishav
您需要查看onSaveInstanceState和onRestoreInstanceState。在我的電話,否則我會詳細說明。 – ziondreamt