2014-06-19 3 views
-1

게임에 간단한 하이 스코어를 저장하려하지만 데이터가 공유 환경 설정에 저장되지 않습니다.하이 스코어가 여전히 공유 환경 설정에 저장되지 않습니다.

SharedPrefManager.java :

package com.divergent.thumbler; 

import android.content.Context; 
import android.content.SharedPreferences; 

// all methods are static , so we can call from any where in the code 
//all member variables are private, so that we can save load with our own fun only 
public class SharedPrefManager { 
//this is your shared preference file name, in which we will save data 
public static final String MY_EMP_PREFS = "MySharedPref"; 

//saving the context, so that we can call all 
//shared pref methods from non activity classes. 
//because getSharedPreferences required the context. 
//but in activity class we can call without this context 
private static Context  mContext; 

// will get user input in below variables, then will store in to shared pref 
private static int   HighScore; 

public static void Init(Context context) 
{ 
    mContext = context; 
} 
public static void LoadFromPref() 
{ 
    SharedPreferences settings  = mContext.getSharedPreferences(MY_EMP_PREFS, 0); 
    // Note here the 2nd parameter 0 is the default parameter for private access, 
    //Operating mode. Use 0 or MODE_PRIVATE for the default operation, 
    settings.getInt("HighScore", HighScore); 

} 
public static void StoreToPref() 
{ 
    // get the existing preference file 
    SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0); 
    //need an editor to edit and save values 
    SharedPreferences.Editor editor = settings.edit(); 

    editor.putInt("HighScore", HighScore); 

    //final step to commit (save)the changes in to the shared pref 
    editor.commit(); 
} 

public static void DeleteSingleEntryFromPref(String keyName) 
{ 
    SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0); 
    //need an editor to edit and save values 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.remove(keyName); 
    editor.commit(); 
} 

public static void DeleteAllEntriesFromPref() 
{ 
    SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0); 
    //need an editor to edit and save values 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.clear(); 
    editor.commit(); 
} 


public static void SetHighScore(int score) 
{ 
    HighScore = score; 

} 

public static int getHighScore() 
{ 
    return HighScore ; 
} 
} 

나는 다음과 같은 기능을 내 최고 점수를 저장하고 :

public void highscoreSaver() { 
    SharedPrefManager.LoadFromPref(); 
    if(finalScore > SharedPrefManager.getHighScore()) { 
     SharedPrefManager.SetHighScore(finalScore);    
     SharedPrefManager.StoreToPref(); 
     } 

} 

그리고이처럼 내 최고 점수를 받고 :

HighScore = SharedPrefManager.getHighScore(); 

있나요 그 내가 잘못하고있는거야? 정수를 반환 SharedPreferences.getInt(String, int), 내가

답변

0

나는 문제가 문서 here 당으로 settings.getInt("HighScore", HighScore);
에있다 생각을 알려 주시기 바랍니다, 두 번째 매개 변수는 같은 것을해야하므로 지정된 키와 아무 선호도가없는 경우 디폴트 값을주는 것입니다 HighScore = settings.getInt("HighScore", 0); 대신

+0

0으로 설정하려고했지만 작동하지 않았습니다. – Divergent

+0

0으로 설정한다는 것은 무엇을 의미합니까? – Mark

+0

"settings.getInt ("HighScore ", 0);" 당신이 저에게 시도해 보라고했는데, 문제를 해결하지 못했습니다. – Divergent

관련 문제