[英]Save Arraylist into sharedpreferences in one activity and load it in recyclerview in another activity
[英]Data on main activity doesn't save / load properly with SharedPreferences
如果您从左到右浏览每张图片,您会在右边的最后一张图片中注意到,测验一的分数变为0 ...这是我的问题
这是我正在与Android Studio一起使用的测验应用程序...我正在尝试使用SharedPreferences,使用putInt和getInt方法将每个测验的得分值放入变量中,并以字符串名称存储,然后onCreate的一部分是使用putInt方法中引用的字符串名称获取getInt ...当用户首次打开应用程序时,主活动应加载全0 ...当用户完成测验时,积分将传回主要活动,并且分数应该保持在该位置……如果用户继续前进,转到另一个屏幕,另一个应用程序或完全关闭该应用程序,这都没有关系,则每次测验的得分都应保持不变,但它们不会: (
当前,主活动上的每个测验按钮都会关闭主活动(finish();),每个测验的最后一个活动会调用主活动并传递得分分数...在主活动关闭之前,我也有每个测验按钮都调用onStop()方法,并且onStop()方法使用SharedPreferences.Editor和putInt方法将每个点值的值放在主要活动上...然后我在onCreate的开头有了SharedPreferences和getInt方法...
所以基本上,我想发生的事情是,每次用户单击测验按钮时,应该使用putInt方法,使用字符串名称以及我正在使用的实际int变量保存每个测验的得分。每个测验的分数,当用户完成测验后回到主要活动时,用户已经计分的任何分数都应通过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
这是每个测验捆绑的外观,除了为相应测验适当命名的变量和字符串名称之外...我开始认为这可能不是获取传递给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
不要手动调用onStop
这些是特殊的方法,您仅在执行一些清理操作时才重写这些方法。
它是活动生命周期方法之一。 您应该将该代码移动到按钮单击事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.