这是我正在研究的测验应用程序,使用Android Studio ...我正在尝试使用SharedPreferences,使用putInt和getInt方法将每个测验得分的值放入变量中,存储在String下name,然后onCreate的一部分是getInt使用putInt m ethods中引用的String名称...当用户第一次打开应用程序时,主活动应加载全0 ...当用户完成一个测验时,点是传回主要活动,积分应该留在那里...如果用户继续前进,转到另一个屏幕,另一个应用程序或完全关闭应用程序无关紧要,每个测验得分应保留,但他们不要't :(
目前,主要活动的每个测验按钮都关闭主要活动(finish();),每个测验的最后一个活动调用主要活动并传递得分...我还有每个测验按钮调用onStop()方法之前main活动关闭,onStop()方法使用SharedPreferences.Editor和putInt方法将每个点值的值放在main活动上......然后我将onAreate的开头的SharedPreferences和getInt方法...
所以基本上,我想要发生的是,每次用户点击测验按钮时,应该使用putInt方法,使用String名称和我实际的int变量来保存每个测验得分。 m用于每个测验的分数,当用户在完成测验后返回主要活动时,用户已经得分的任何分数都应该通过getInt方法自动加载...请参阅下面的图片,了解我的程序中的一些编码。 ..让我知道是否有人需要看到更多的代码,我希望有人可以帮助我! :)
我已经尝试了以前的人建议的内容,但它仍然无效......请参阅下面的部分代码并告诉我需要修复的内容:
//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
这是每个测验Bundle的样子,除了相应的测验适当命名的变量和字符串名称...我开始认为这可能不是获得int值的最佳方法从另一个活动传递到主要活动,但我想不出另一种方式去做...在这个例子中,我试图保存我从测验中得到的numberQuiz#Points的值,并从所有其他int变量获取点的值,然后在应用程序的主屏幕上显示所有...但这不起作用:(
以下是测验按钮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
这就是给定测验的最后一个活动的代码:(在这种情况下,测验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
答案 0 :(得分:2)
不要手动调用onStop
这些是您仅覆盖以执行某些清理的特殊方法。
它是活动生命周期方法之一。您应该将该代码移动到按钮单击事件。