2016-12-27 10 views
-1

私の新しいアプリでは、ボタンをクリックするとカウンタが増えます。私はsharedPreferencesでハイスコアを保存したいので、次回のアプリ起動時にスコアが保存されて表示されます。問題は、他の答えられた質問でさえ、私が実際にそれを働かせることはないということです。android - sharedPreferencesでintを保存する

package com.example.test; 

public class MainActivity extends ActionBarActivity { 

    public int score = 0; 
    public int highscore = 0; 
    TextView tvscore; 


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

     TextView tvhighscore= (TextView) findViewById(R.id.highscore); 
     tvscore = (TextView) findViewById(R.id.score); 
     Button count = (Button) findViewById(R.id.button1); 

     tvhighscore.setText(String.valueOf(highscore)); 

     SharedPreferences prefs = this.getSharedPreferences("score", Context.MODE_PRIVATE); 
     Editor editor = prefs.edit(); 
     editor.putInt("score", 0); 
     editor.commit(); 



    } 

    public void onClick (View view) { 
     score++; 
     tvscore.setText(String.valueOf(score)); 

     SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
     int highscore = prefs.getInt("score", 0); 
    } 


} 
+0

ボタンをクリックすると 'SharedPreferences'にスコアを保存する必要があります。 –

+0

@ρяσѕρєяKが正しいです。さらに、共有設定ファイルに同じ名前を使用していることを確認してください。 –

+0

私は '' score ''と同じ名前を使用していませんか? –

答えて

0

まず、書き込みとクエリの際に共有の環境設定に同じキーを使用する必要があります。次にonclickで、あなたはprefsにスコアを格納し、再度それを問い合わせる必要はありません。ここで更新されたコードがあります:あなたはあなたのコードに多少の誤差があります

public class MainActivity extends ActionBarActivity { 

     public int score = 0; 
     public int highscore; 
     TextView tvscore, tvhighscore; 

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

      tvhighscore= (TextView) findViewById(R.id.highscore); 
      tvscore = (TextView) findViewById(R.id.score); 
      Button count = (Button) findViewById(R.id.button1); 


      SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
      highscore = prefs.getInt("high_score", 0); 
      tvhighscore.setText(String.valueOf(highscore)); 
     } 

     public void onClick (View view) { 
      score++; 
      tvscore.setText(String.valueOf(score)); 
      if (score > highscore) { 
       highscore = score; 
       SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
       prefs.edit().putInt("high_score", highscore).apply(); 
       tvhighscore.setText(String.valueOf(highscore)); 
      } 
     } 
    } 
+0

それは動作します、ありがとう! –

0

まずエラーは、SP

SharedPreferencesに別の名前を使用していることであるが​​を介してアクセスされています。あなたのコードには、myPrefsKeymyPrefsKeyの2つがあります。必ず同じものを使用するか、値が見つからないようにしてください。

はあなたが同じ名前highscoreintをキャストしているコードにしてonclick方法で

どちらも2回同じint型の名前を使用していることです。これが許可されていない

第三は論理である:

何をやっている:

  • activity起動時に値を保存します。
  • あなたは次の操作を行う必要がありますがbuttonクリック

上の値を読む:

  • あなたはbuttonクリックの内側に使用したとしてtextview'sテキストを設定しているのgetIntコードを使用してonCreate方法で値を読みますそれ。
  • 値を増やした後にbuttonをクリックして値を保存します。

このようにすれば、コードが機能します。例以下は

package com.example.test; 

public class MainActivity extends ActionBarActivity { 

    public int score = 0; 
    public int highscore = 0; 
    TextView tvscore; 
    SharedPreferences prefs; 

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

     TextView tvhighscore= (TextView) findViewById(R.id.highscore); 
     tvscore = (TextView) findViewById(R.id.score); 
     Button count = (Button) findViewById(R.id.button1); 
     //here you retrieve the value of the highscore 
     prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
     int highscore = prefs.getInt("score", 0); 
     tvhighscore.setText(String.valueOf(highscore)); 



    } 

    public void onClick (View view) { 
     score++; 
     //here you save the value of the score in your pref 
     tvscore.setText(String.valueOf(score)); 
     Editor editor = prefs.edit(); 
     editor.putInt("score", score); 
     editor.commit(); 
    } 

} 

知らんこれは、あなたが探していたまさにですが、これはあなたが論理を理解するに役立つはず:)

幸運ならば!

+0

私はそれを試してみる、ありがとう! –

0

spを管理するクラスを作成することをお勧めします。 私はあなたの下に例を残す。

public class SharedPrefsManager { 
    private static final String USER_CODE = "userCode"; 
    private static SharedPreferences sharedPreferences; 
    private static SharedPreferences.Editor prefEditor; 

    private static void setPreferences(Context context) { 
     if (context == null) { 
      context = Application.getContext(); 
     } 
     sharedPreferences = context.getSharedPreferences("APP_NAME", 0); 
    } 

    public static int getCodigoUsuario(Context context) { 
     setPreferences(context); 
     return sharedPreferences.getString(USER_CODE, 0); 
    } 

    public static void setCodigoUsuario(int userCode, Context context) { 
     setPreferences(context); 
     prefEditor = sharedPreferences.edit(); 
     prefEditor.putInt(USER_CODE, userCode); 
     prefEditor.commit(); 
    } 
    } 

SAVE:SharedPrefsManager.setCodigoUsuario(13、コンテキスト);

GET SharedPrefsManager.getCodigoUsuario(context);

+1

そしてこのクラスで私はそれをしようとするとそれを保存しますか? –

+0

SharedPrefsManager.setCodigoUsuario(user、context); –

関連する問題