2012-03-17 3 views
1

그래서 sharedPreferences를 사용하여 가장 높은 점수를 얻으려고하고 있지만 문제가 있습니다. 가장 높은 점수를 받고 그것을 표시하는 방법을 어떻게 설정할지 모르겠습니다.공유 환경 설정으로 최고 점수 표시?

내가 지금 가지고있는 것은 현재 플레이어가받는 현재 점수 만 표시합니다. 여기에 지금까지 그 나던 일이 무엇이에 의해

public class GameOptions extends Activity { 
int theScore; 
TextView highScore; 
public static String filename = "MyHighScore"; 
SharedPreferences spHighScore; 
int dataReturned; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.in_game_menu); 
    TextView tvTheScore = (TextView) findViewById(R.id.textView2); 
    TextView highScore = (TextView) findViewById(R.id.high_score); 
    Bundle gotScore = getIntent().getExtras(); 
    theScore = gotScore.getInt("scoreKey"); 
    //String thisIsTheScoreToDisplay = theScore.toString(); 
    tvTheScore.setText("SCORE: "+theScore); 

    spHighScore = getSharedPreferences(filename, 0); 
    SharedPreferences.Editor editor = spHighScore.edit(); 

    editor.putInt("highScoreKey", theScore); 
    editor.commit(); 

    int dataReturned = spHighScore.getInt("highScoreKey", 0); 
    highScore.setText("" + dataReturned); 
+0

btw 이것은 게임용이며 단지 int 인 # 1 최고 점수만을 저장하려고합니다. 어떤 도움을 주셔서 다시 한번 감사드립니다. – alexward1230

답변

3

을 당신은 당신의 점수

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
    SharedPreferences.Editor prefsEditor = myPrefs.edit(); 
    prefsEditor.putString(MY_SCORE_KEY, HIGHEST_SCORE); 
    prefsEditor.commit(); 

을 저장하고 난이 당신을 도울 것입니다 희망이

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
    String highestScore = myPrefs.getString(MY_SCORE_KEY, "nothing"); 

같이 얻을 수 있습니다.

2

바로 전에 높은 점수를 저장하기 전에 환경 설정에 저장된 현재 높은 점수를 가져 와서 파일이 le인지 확인하십시오 최고 점수보다 높거나 낮습니다. 공유 환경 설정에 새 환경을 저장하거나 이전 환경을 가장 높은 환경으로 두십시오.