2012-03-17 9 views
1

私はsharedPreferencesで最高のスコアを保存しようとしていますが、問題が発生しています。私は最高のスコアを出してそれを表示するように設定する方法を私は知らない。共有設定でハイスコアを表示しますか?

私が今行っているのは、プレーヤーが受け取った現在のスコアのみが表示されます。ここで私は今のところそのdoesntの仕事を持っているものである。このことにより、

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

直前に、高スコアを保存します。設定に保存されている現在の高スコアを取り出し、それがファイルかどうかを確認します最高得点よりも高い。それが少ない場合は、新しいものを共有設定に保存してください。それ以外の場合は、過去のものを最高のものとして残してください。

関連する問題