如何修复“数据未保存”?

时间:2019-07-28 18:01:47

标签: java

我正在做一个新的问答游戏。我正在尝试解决带有分数的保存数据问题。当我打开活动时,分数是可见的,但是当我第二次打开相同的活动时,我失去了分数,所有数据都返回0。该如何解决?

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

私人无效的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;
    }


}

//我希望数据已保存,但与我期望的不一样...

//我修改了代码,就像这样,但问题仍然存在:  私人空白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 ();


}

//,然后:   私人空白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 );
    }

}

2 个答案:

答案 0 :(得分:0)

您可以代替apply()尝试致电commit()吗?

editor.commit();

请注意,commit()是一个阻塞调用,应避免在UI线程中使用。如果commit()无法正常工作,请通知我。

顺便说一下,如何获取SharedPreferences对象还不清楚。应该是:

SharedPreferences pref = getApplicationContext().getSharedPreferences("XYZ", MODE_PRIVATE); 

答案 1 :(得分:0)

您没有正确初始化您的SharedPreferences,您应该拨打电话:

MyDF = pd.read_excel('/FullPathToYourExcelFile/MyExcelFile.xlsx', names=['Amount','Material_1','Material_2','Material_3','Material_4'], header=None)

Here's a good SharedPreferences example.