2017-08-11 3 views
0

J'ai un problème avec l'enregistrement de la chaîne dans SharedPreference. Cette classe enregistre le numéro de carte et récupère la chaîne à partir de SharedPreference. this my classe MyShrePreference. Je ne sais pas pourquoi cette chaîne ne sauvegarde pas dans SharedPreference.Je ne peux pas enregistrer les données dans Shareprefence Chaîne

public class MySharepreference { 

    public static final String PREFS_NAME = "POSITION"; 
    public static final String POSITION = "current"; 
    public String nameString; 

    public MySharepreference() { 
     super(); 
    } 


    public void saveNumberCard(Context context, String position) { 
     SharedPreferences settings; 
     SharedPreferences.Editor editor; 
     settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     editor = settings.edit(); 
     editor.putString(POSITION, position); 
     editor.commit(); 
    } 

    public String getNumberCard(Context context) { 
     SharedPreferences sharedPreferences; 
     sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     if (sharedPreferences.contains(POSITION)) { 
      nameString = sharedPreferences.getString(POSITION, ""); 
     } 
     return nameString; 
    } 

} 

sauver à SharedPreference à l'adaptateur

onBindViewHolder

@Override 
    public void onBindViewHolder(CardViewHolder holder, final int position) { 

    mySharepreference = new MySharepreference(); 

     if (position == lastCheckedPos) { 
      mySharepreference.saveNumberCard(mContext, card.getNumberCard()); 
} 
+0

mieux expliquer votre problème. –

+1

Pouvez-vous ajouter le code où vous appelez cette méthode –

+0

Votre code est correct, Il suffit de supprimer ** if (sharedPreferences.contains (POSITION)) ** –

Répondre

0
public class SharedPreferenceUtils { 
    private static final Context context = MyApplication.getInstance(); 
    private static String SHARED_PREFERENCES_FILE_NAME = "myapplicationpref"; 
    private static SharedPreferences preference; 

    public static SharedPreferences getSharedPrefrence() { 
     if (preference == null) { 
      preference = context.getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, 
        Context.MODE_PRIVATE); 
     } 
     return preference; 

    } 


    public static String getStringValueFromSharedPrefarence(String key, String defaultValue) { 
     return getSharedPrefrence().getString(key, defaultValue); 

    } 
    public static boolean getBooleanValueFromSharedPrefarence(String key, boolean defaultValue) { 
     return getSharedPrefrence().getBoolean(key, defaultValue); 
    } 

    public static boolean setBooleanValueToSharedPrefarence(String key, boolean value) { 
     return getSharedPrefrence().edit().putBoolean(key, value).commit(); 

    } 
    public static boolean setStringValueToSharedPrefarence(String key, String value) { 
     return getSharedPrefrence().edit().putString(key, value).commit(); 

    } 
    public static boolean setStringSetToSharedPrefarence(String key, Set<String> value) { 
     return getSharedPrefrence().edit().putStringSet(key, value).commit(); 

    } 

} 
+0

reste de votre code est bon, cette ligne fuit contexte privé statique final Context context = MyApplication.getInstance(); , à la place, vous pouvez utiliser un motif de conception singleton et lui passer le contexte de l'application. – NaserShaikh

0

changer le code mContext à getApplicationContext(), comme

 mySharepreference.saveNumberCard(getApplicationContext(), card.getNumberCard());