Я пытаюсь сравнить 2 оценки, которые я сделал oldScore и best_score (оба в Main_Screen). Я думаю, проблема в том, что ints не сохраняются должным образом. Ошибок нет, но если 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();
}
}
Каково ваше первоначальное значение 'best_score_number_tv'? – joao2fast4u
0 - это оригинальное значение @ joao2fast4u – Celeriac
Почему вы не устанавливаете текст best_score_number_tv из настроек сразу после входа в MainScreen? Таким образом, значение, которое считывается из этого TextView (oldscore), всегда равно нулю). Я прав? – joao2fast4u