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