试图比较分数

Trying to compare scores

我正在尝试比较 oldScore 和 best_score(均在 Main_Screen 中)的 2 个乐谱。我认为问题是整数没有正确保存。没有错误,但是如果 best_Score 低于 old_score,即使 best_score 应该高于 old_score,它仍然会在文本视图中更改它。希望我能从中吸取教训:)

Main_Screen

public class Main_Screen extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        getSupportActionBar().hide();

        //SETS CUSTOM FONT
        TextView main_screen_titleone = (TextView)findViewById(R.id.main_screen_titleone);
        TextView main_screen_titletwo = (TextView)findViewById(R.id.main_screen_titletwo);
        TextView best_score_tv = (TextView)findViewById(R.id.best_score_tv);
        TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
        Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/LemonMilk.otf");
        main_screen_titleone.setTypeface(myCustomFont);
        main_screen_titletwo.setTypeface(myCustomFont);
        best_score_tv.setTypeface(myCustomFont);
        best_score_number_tv.setTypeface(myCustomFont);
        //******************;

        int best_score = retrieveInt("BEST_SCORE");

        int oldScore = Integer.valueOf(best_score_number_tv.getText().toString());

        if (best_score > oldScore){

            best_score_number_tv.setText(best_score + "");
        }

}


    public void startTheGame(View view){
    Intent intent = new Intent(this, press_screen.class);
    startActivity(intent);
    }

    public int retrieveInt(String key){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        return sp.getInt(key, 0);
    }

Press_Screen

public class press_screen extends ActionBarActivity {

    private int time_left;
    private int amountOfTapsNumber;
    private int bestScore;

    TextView amountOfTaps;
    TextView timeLeftNumber;
    TextView time_left_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_press_screen);
        getSupportActionBar().hide();

        amountOfTaps = (TextView)findViewById(R.id.amount_of_taps);
        timeLeftNumber = (TextView)findViewById(R.id.time_left_number_tv);
        time_left_tv = (TextView)findViewById(R.id.time_left_tv);
        Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/LemonMilk.otf");
        amountOfTaps.setTypeface(myCustomFont);
        timeLeftNumber.setTypeface(myCustomFont);
        time_left_tv.setTypeface(myCustomFont);

        timer.start();

    }

    //Create Timer


    CountDownTimer timer = new CountDownTimer(21000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv);
            time_left = Integer.valueOf(timeLeftNumber.getText().toString()) - 1;
            timeLeftNumber.setText(time_left + "");
        }


        @Override
        public void onFinish() {

            if(amountOfTapsNumber > bestScore){
                bestScore = amountOfTapsNumber;
                saveInfo("BEST_SCORE", bestScore);
            }

            TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);

            Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
            startActivity(goBackToMainActivity);

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_press_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void triggerTapOnMainButton(View view) {
        amountOfTaps = (TextView) findViewById(R.id.amount_of_taps);
        amountOfTapsNumber = Integer.valueOf(amountOfTaps.getText().toString()) + 1;
        amountOfTaps.setText(amountOfTapsNumber + "");
    }

    public void saveInfo(String key, int bestScore){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("BEST_SCORE", bestScore);
        edit.commit();
    }
}

当您的计时器结束时,您将 Intent 上的 tapsNumber 发送到 MainActivity,如下所示:

Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
Bundle bundle = new Bundle();
bundle.putInt("lastScore", amountOfTapsNumber);
goBackToMainActivity.putExtras(bundle);
startActivity(goBackToMainActivity);

然后,在您的 MainActivity onCreate() 中,检查您的 Intent 是否具有 oldScore。如果是,请将其与 best_score.

进行比较
int lastScore = 0;
if(getIntent() !=null && getIntent().getIntExtra("lastScore",0)>0)
      lastScore = getIntent().getIntExtra("lastScore",0);

//comparing the lastScore with the bestScore
if (lastScore > best_Score){
      best_score_number_tv.setText(lastScore + "");
}
else{
     best_score_number_tv.setText(best_Score + "");
}

这样,如果您的 lastScore 高于旧 best_score,您的 bestScore 将被更新。