【问题标题】:How to fix "Data not saved"?如何修复“数据未保存”?
【发布时间】:2019-07-28 18:01:47
【问题描述】:

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

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

private void 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和formalLabel,但是没有保存

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 );
    }

}

【问题讨论】:

    标签: java


    【解决方案1】:

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

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

    Here's a good SharedPreferences example.

    【讨论】:

    • 我按照你说的试过了,但问题仍然存在
    【解决方案2】:

    你可以尝试拨打commit()而不是apply()吗?

    editor.commit();
    

    请注意commit() 是一个阻塞调用,应避免在 UI 线程中使用。如果commit() 不起作用,请告诉我。

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

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

    【讨论】:

    • 我按照你说的试过了,但问题仍然存在
    猜你喜欢
    • 1970-01-01
    • 2022-12-23
    • 2020-09-28
    • 1970-01-01
    • 2020-05-08
    • 2020-11-23
    • 2018-01-30
    • 2016-05-19
    • 1970-01-01
    相关资源
    最近更新 更多