2016-03-05 3 views
0

Как сохранить лучшие первые три балла или только лучший результат, я новичок в программировании. У меня есть оценка, но как определить новый счет и старый счет.как сохранить первые три лучшие оценки

public class HighScores extends Activity { 

    private TextView thighscore1; 
    private TextView thighscore2; 
    private TextView thighscore3; 
    public TextView name; 
    public int highscore1 =0; 
    public int highscore2 =0; 
    public int highscore3 =0; 

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

     name = (TextView) findViewById(R.id.Names); 
     thighscore1 = (TextView) findViewById(R.id.highscore1); 
     thighscore2 = (TextView) findViewById(R.id.highscore2); 
     thighscore3 = (TextView) findViewById(R.id.highscore3); 

     SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", 
       Context.MODE_PRIVATE); 

     int score = prefs.getInt("key", 0); //0 is the default value 
     thighscore1.setText("" + score); 
     if(score > highscore2) { 
      highscore1 = score; 
      thighscore1.setText("" + highscore1); 
     }else{ 
      highscore1=highscore1; 
     } 
     if(score > highscore3 && score < highscore1){ 
      highscore2 = score; 
      thighscore2.setText("" + highscore2); 
     }else{ 
      highscore2 = highscore2; 
     } 
     if (score > 0 && score < highscore2){ 
      highscore3 = score; 
      thighscore3.setText("" + highscore3); 
     }else{ 
      highscore3 = highscore3; 
     } 

     SharedPreferences sp = this.getSharedPreferences("MyKey",0); 
     String data = sp.getString("tag", ""); 
     name.setText(""+ data); 
    }} 

ответ

0

Для установки порезов Предпочтение:

SharedPreferences.Editor editor = getSharedPreferences("PreferenceName",MODE_PRIVATE).edit(); 
editor.putInt("Score1", highscore1); 
editor.putInt("Score2", highscore2); 
editor.putInt("Score3", highscore3); 
editor.apply(); 

Для получения оценки от Preference:

SharedPreferences prefs = getSharedPreferences("PreferenceName",MODE_PRIVATE); 
if(prefs.getString("text", null) != null) { 
    int score1 = prefs.getInt("Score1", 0); // 0 is the default value 
    int score2 = prefs.getInt("Score2", 0); 
    int score3 = prefs.getInt("Score3", 0); 
} 

Больше информации о SharedPreferences

Чтобы удалить любое значение из Preference

editor.remove("highscore1"); // will delete highscore1 
+0

спасибо, но я не могу решить мою проблему, я экономить только новые Гол. Как определить более высокий балл private void HighScore() { SharedPreferences prefs = this.getSharedPreferences ("myPrefsKey", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putInt («ключ», номер); editor.commit(); –

+0

Я не могу точно понять, что вы хотите, но если вы хотите обновить предыдущий рекордер, я просто отредактировал свой ответ. – maRShmallow

+0

Я знаю только: int score1 = prefs.getInt ("Score1", 0); // 0 является значением по умолчанию, как я получаю score2 и score3 SharedPreferences.Editor editor = prefs.edit(); editor.putInt («ключ», счет 1); editor.commit(); –

0

я решил мою проблему со счетом также иметь название:

if (number > prefs.getInt("key", 0)) { 
         editor.putInt("key", number); 
         editor.putString("tag", value); 

        } else if (number < prefs.getInt("key", 0)) { 
         if (number > prefs.getInt("key1", 0)) { 
          editor.putInt("key1", number); 
          editor.putString("tag1", value1); 
         } 
        } 
        if (number < prefs.getInt("key1", 0)) { 
         editor.putInt("key2", number); 
         editor.putString("tag2", value2); 
        }