![](/img/trans.png)
[英]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.