试图比较分数

时间:2015-03-14 21:13:07

标签: android if-statement sharedpreferences

我正在尝试比较我使用oldScore和best_score的两个分数(都在Main_Screen中)。我认为问题在于整数没有正确保存。没有错误但是如果best_Score低于old_score,它仍会在textview中更改它,即使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();
    }
}

1 个答案:

答案 0 :(得分:1)

当您的计时器结束时,您将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()中,检查您的意图是否包含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将会更新。