If you look through each picture, from left to right, you'll notice on the last picture on the right, the points for Quiz One goes to 0... this is my problem的主要活动中的数据没有正确保存/负载SharedPreferences
这是我的工作,与Android Studio中的一个测验应用程序......我试图使用SharedPreferences,使用putInt和getInt方法将每个测验得分的值存入变量,存储在一个String名称下,然后onCreate的一部分是getInt使用putInt m方法中引用的字符串名称...主要活动应该在用户第一时加载所有0第一次打开应用程序...当用户完成测验时,积分将返回到主要活动,积分应保持在那里......如果用户继续前进,转到另一个屏幕,另一个应用程序,或完全关闭应用程序,每个测验得分应保持不变,但他们不:(
当前,主要活动上的每个测验按钮关闭主要活动(完成(); ),并且每个测验的最后一个活动调用主要活动并通过积分评分...我还有每个测验按钮在主要活动关闭前调用onStop()方法,并且onStop()方法使用SharedPreferences.Editor和putInt方法在主要活动的每个点值的值...然后我有SharedPreferences和getInt方法朝着onCreate的开始...
所以基本上,我想要发生的是,每次用户点击测验按钮,使用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
这就是每个测验Bundle的样子,除了为相应的测验命名的变量和字符串名称之外......我开始想,也许这不是获取传递给int值的最佳方式主要活动从另一个活动,但我想不出另一种方式来做到这一点...在这种情况下,我试图保存麻木的价值erQuiz#我马上从测验中获得的点数,并从所有其他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
使用userDefined函数更新MainActivity中的分数来更新sharedPreferences中的分数 – Sackurise
仍在挣扎:(你能提供一个userDefined函数的例子供我使用吗?我仍然有点新Android Studio –
什么是目前的情况现在 – meda