如果你仔细查看每张图片,从左到右,你会注意到右边的最后一张图片,Quiz One的点数会变为0 ......这是我的问题
This is a quiz app that i'm working on, with Android Studio... I am trying to use SharedPreferences, using putInt and getInt methods to put the value of points scored for each quiz into variables, stored under a String name, then part of onCreate is to getInt using the String name referenced in the putInt m ethods... main activity should load with all 0s when user first opens the app for the first time... when user completes a quiz, points are passed back to main activity, and the points should STAY there... doesn't matter if user moves on, goes to another screen, another app, or close the app altogether, the points scored for each quiz should stay, but they don't :(
这是我正在研究的测验应用程序,使用Android Studio ...我正在尝试使用SharedPreferences,使用putInt和getInt方法将每个测验得分的值放入变量中,存储在String名称下,然后onCreate的一部分是getInt使用putInt m ethods中引用的String名称...当用户第一次打开应用程序时,主活动应加载全0 ...当用户完成测验时,点数将被传递回主要活动,点应该留在那里...如果用户继续前进,转到另一个屏幕,另一个应用程序或完全关闭应用程序无关紧要,每个测验的得分应保留,但他们不会: (
Currently, each quiz button on main activity closes main activity ( finish(); ), and the last activity of each quiz calls main activity and passes points scored... I also have each quiz button calling onStop() method before main activity closes, and the onStop() method uses SharedPreferences.Editor and putInt methods to put the value of each point value on main activity... and then I have SharedPreferences and getInt methods towards the beginning of onCreate...
目前,主要活动上的每个测验按钮关闭主要活动(finish();),每个测验的最后一个活动调用主要活动并传递得分...我还有每个测验按钮在主要活动关闭之前调用onStop()方法,onStop()方法使用SharedPreferences.Editor和putInt方法将每个点值的值放在主活动上......然后我将onCreate的开头的SharedPreferences和getInt方法...
So basically, what i want to have happen, is every time user clicks on a quiz button, the points scored for each quiz should be saved, using putInt methods, using a String name, and the actual int variable that i'm using for the points for each quiz, and when user goes back to main activity after completing a quiz, any points that user already scored should automatically load, via getInt methods... see picture below for some of the coding in my program... let me know if anyone needs to see more code, and I hope someone can help me out! :)
所以基本上,我想要发生的是,每当用户点击测验按钮时,应该保存每个测验得分,使用putInt方法,使用String名称,以及我用于实际的int变量每个测验的分数,当用户在完成测验后返回主要活动时,用户已经得分的任何分数都应该通过getInt方法自动加载...请参阅下面的图片,了解我程序中的一些编码...我知道是否有人需要查看更多代码,我希望有人可以帮助我! :)
不同的代码块,最重要的代码块,与我遇到的问题有关或有关
I've tried what previous people have suggested, but it's still not working... see parts of my code below and let me know what needs fixed:
我已经尝试了以前的人建议的内容,但它仍然无法正常工作...请参阅下面的部分代码并告诉我需要修复的内容:
//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();
//Get the value of points from all other quizzes with getInt from SharedPreferences
numberQuizTwoPoints = shareInt.getInt("quizTwoPointsYeah", 0);
numberQuizThreePoints = shareInt.getInt("quizThreePointsYeah", 0);
numberQuizFourPoints = shareInt.getInt("quizFourPointsYeah", 0);
numberQuizFivePoints = shareInt.getInt("quizFivePointsYeah", 0);
numberQuizSixPoints = shareInt.getInt("quizSixPointsYeah", 0);
numberQuizSevenPoints = shareInt.getInt("quizSevenPointsYeah", 0);
numberQuizEightPoints = shareInt.getInt("quizEightPointsYeah", 0);
numberQuizNinePoints = shareInt.getInt("quizNinePointsYeah", 0);
numberQuizTenPoints = shareInt.getInt("quizTenPointsYeah", 0);
//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 if
This is what each quiz Bundle looks like, with the exception of the variables and string names named appropriately for the corresponding quiz... I'm starting to think maybe this isn't the best way to get an int value that is passed to Main Activity from another activity, but I can't think of another way to do it... in this instance, I'm trying to save the value of numberQuiz#Points that I'm getting from a quiz right away, and getting the value of points from all other int variables, and then displaying all on the main screen of the app... but this isn't working :(
这是每个测验Bundle看起来的样子,除了相应的测验适当命名的变量和字符串名称...我开始认为这可能不是获取传递给的int值的最佳方法来自另一个活动的主要活动,但我想不出另一种做法...在这个例子中,我试图保存numberQuiz#Points的价值,我现在从测验中获得,并获得来自所有其他int变量的点的值,然后在应用程序的主屏幕上显示所有...但这不起作用:(
Here is an example of what the code behind a quiz button setOnClickListener looks like:
以下是测验按钮setOnClickListener背后的代码示例:
//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
And this is what the code looks like for the last activity of a given quiz: (In this case, the last activity for Quiz 1, the activity that is passing points back to MainAcvitity:
这就是给定测验的最后一个活动的代码:(在这种情况下,测验1的最后一个活动,即将点传递回MainAcvitity的活动:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuizOnePartFour extends AppCompatActivity
{
//Create variables for button and TextViews
TextView totalPointsQuizOneTextView;
Button returnToMainOne;
int actualPointsAllQuizOne;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_one_part_four);
Bundle bundleFour = getIntent().getExtras();
actualPointsAllQuizOne = bundleFour.getInt("CarryOverThreeQuizOne");
//Assign variables to appropriate buttons and TextViews
totalPointsQuizOneTextView = (TextView) findViewById(R.id.txtTotalPointsQuizOne);
returnToMainOne = (Button) findViewById(R.id.btnReturnToMainOne);
totalPointsQuizOneTextView.setText(String.format("%d", actualPointsAllQuizOne));
returnToMainOne.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Return to main menu screen
Bundle bundleFive = new Bundle();
bundleFive.putInt("PointsFromAllQuizOne", actualPointsAllQuizOne);
Intent directToMainMenuOne = new Intent();
directToMainMenuOne.setClass(getBaseContext(), MainMenu.class);
directToMainMenuOne.putExtras(bundleFive);
startActivity(directToMainMenuOne);
finish();
}//end void onClick returnToMainOne
});//end setOnClickListener returnToMainOne
}//end void onCreate QuizOnePartFour
}//end class QuizOnePartFour
1 个解决方案
#1
2
Don't manually call onStop
these are special methods that you override only to perform some clean ups.
不要手动调用onStop,这些是您仅覆盖以执行某些清理的特殊方法。
It is one of the activity life cycle method. You should move that code to the button click event.
它是活动生命周期方法之一。您应该将该代码移动到按钮单击事件。
#1
2
Don't manually call onStop
these are special methods that you override only to perform some clean ups.
不要手动调用onStop,这些是您仅覆盖以执行某些清理的特殊方法。
It is one of the activity life cycle method. You should move that code to the button click event.
它是活动生命周期方法之一。您应该将该代码移动到按钮单击事件。