gpt4 book ai didi

java - 如何修复 "Data not saved"?

转载 作者:行者123 更新时间:2023-12-02 02:28:06 26 4
gpt4 key购买 nike

我正在制作一个新的问答游戏。我正在尝试解决分数保存数据的问题。当我打开 Activity 时,分数是可见的,但当我第二次打开同一个 Activity 时,我会丢失分数并且所有数据返回到 0。我该如何修复它?

我使用 SharedPreferences 来保存数据,因为我不使用数据库服务器。

私有(private)无效Punteggio(){

    // TODO: initialization 

final int score = getIntent ( ).getIntExtra ( Home.DATA_Score , 0 );
last_score = score;
last_score_Label.setText ( "Ultimo punteggio: " + last_score );


// TODO: Miglior punteggio e ultimo punteggio
SharedPreferences settings = getSharedPreferences ( "GAME_DATA" , MODE_PRIVATE );
highscore = settings.getInt ( "HIGHSCORE" , 0 );


// TODO: Saving highscore
// Last score isn't saved but highscore is saved
if ( score > highscore ) {
highscore_Label.setText ( "Miglior punteggio: " + score );

// Salvataggio
SharedPreferences.Editor editor = settings.edit ( );
editor.putInt ( "HIGHSCORE" , score );
editor.putInt ( "LAST_SCORE", last_score );
editor.apply ( );
} else {
highscore_Label.setText ( "Miglior punteggio: " + highscore );
}

}

//我想保存graphicAvatar和formaLabel,但它们没有保存

private void showGrafico(){

final int number = getIntent ().getIntExtra ( Home.DATA_Avatar, 0 );
number_avatar = number;
switch (number){
case 1: graphicAvatar.setImageResource ( R.drawable.negative );
formaLabel.setText ( "Stato di forma: Pessimo" );
break;
case 2: graphicAvatar.setImageResource ( R.drawable.normal );
formaLabel.setText ( "Stato di forma: Normale" );
break;
case 3: graphicAvatar.setImageResource ( R.drawable.positive );
formaLabel.setText ( "Staot di forma: Ottimo" );
break;
}


}

//我希望数据已保存,但它并不像我预期的那样...

//我修改了代码,是这样的,但问题仍然存在: 私有(private)无效 showGrafico(){

    final int number = getIntent ().getIntExtra ( Home.DATA_Avatar, 0 );
switch (number){
case 1: graphicAvatar.setImageResource ( R.drawable.negative );
formaLabel.setText ( "Stato di forma: Pessimo" );
break;
case 2: graphicAvatar.setImageResource ( R.drawable.normal );
formaLabel.setText ( "Stato di forma: Normale" );
break;
case 3: graphicAvatar.setImageResource ( R.drawable.positive );
formaLabel.setText ( "Staot di forma: Ottimo" );
break;
}

// TODO: Miglior punteggio e ultimo punteggio

SharedPreferences pref = getApplicationContext().getSharedPreferences("DATA_AVATAR", MODE_PRIVATE);
number_avatar = pref.getInt ( "NUMBER" , 0 );

SharedPreferences.Editor editor = pref.edit ( );
editor.putInt ( "NUMBER" , number_avatar );
editor.commit ();


}

//然后还有: 私有(private)无效 showPunteggio ( ) {

    // TODO: inizializzazione variabili

final int score = getIntent ( ).getIntExtra ( Home.DATA_Score , 0 );
last_score = score;
last_score_Label.setText ( "Ultimo punteggio: " + last_score );




// TODO: Miglior punteggio e ultimo punteggio
SharedPreferences settings = getApplicationContext().getSharedPreferences("GAME_DATA",0);
highscore = settings.getInt ( "HIGHSCORE" , 0 );


// TODO: Salvataggio Miglior Punteggio
if ( score > highscore ) {
highscore_Label.setText ( "Miglior punteggio: " + score );

// Salvataggio
SharedPreferences.Editor editor = settings.edit ( );
editor.putInt ( "HIGHSCORE" , score );
editor.commit ( );
} else {
highscore_Label.setText ( "Miglior punteggio: " + highscore );
}

}

最佳答案

您没有正确初始化 SharedPreferences - 您应该调用:

getApplicationContext().getSharedPreferences("GAME_DATA",0);

Here's a good SharedPreferences example.

关于java - 如何修复 "Data not saved"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com