How to display SharedPreference stored data in Fragment?

后端 未结 2 1102
南旧
南旧 2020-12-22 13:41

I stored values in SharedPreference and now I want to access all that in Fragment. When I tried to run my App, it crashed.

public class Credentials extends F         


        
相关标签:
2条回答
  • 2020-12-22 13:54

    There is problem with your receiving method.

    public abstract String getString (String key, String defValue)

    Retrieve a String value from the preferences.

    Parameters

    key : The name of the preference to retrieve.
    defValue : Value to return if this preference does not exist.

    Returns

    Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.

    So when you use pref.getString() method, you have to store return value to something.

    Try below example.

    String username = pref.getString("username", "EMPTY");
    Toast.makeText(getActivity(), "Value : "+username, Toast.LENGTH_SHORT).show();
    
    0 讨论(0)
  • 2020-12-22 14:11

    I have Created a Util Class for SharedPreference, Hope It will help you.

    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    
    public class SharedPrefrenceUtils {
        public static final String SHARED_PREFERENCE_TAG="TAG_NAME";
    
        private SharedPrefrenceUtils(){
             throw new AssertionError();
        }
    
        public static String getString(Context mContext, String key){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getString(key, null);
        }
    
        public static String getString(Context mContext, String key,  String defaultValue){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getString(key, defaultValue);
        }
    
        public static void putString(Context mContext, String key, String value ){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(key, value);
            editor.commit();
        }
    
    
        public static int getInt(Context mContext, String key){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getInt(key, 0);
        }
    
        public static int getInt(Context mContext, String key,  int defaultValue){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getInt(key, defaultValue);
        }
    
        public static void putInt(Context mContext, String key, int value ){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putInt(key, value);
            editor.commit();
        }
    
        public static long getLong(Context mContext, String key){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getLong(key, 0);
        }
    
        public static long getLong(Context mContext, String key,  long defaultValue){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getLong(key, defaultValue);
        }
    
        public static void putLong(Context mContext, String key, long value ){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putLong(key, value);
            editor.commit();
        }
    
    
        public static boolean getBoolean(Context mContext, String key){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getBoolean(key, false);
        }
    
        public static boolean getBoolean(Context mContext, String key,  boolean defaultValue){
            SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
            return pref.getBoolean(key, defaultValue);
        }
    
        public static void putBoolean(Context mContext, String key, boolean value ){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean(key, value);
            editor.commit();
        }
    
        public static void remove(Context mContext, String key){
                SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.remove(key);
                editor.commit();
        }
    
        public static void clear(Context mContext){
                SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.clear();
                editor.commit();
        }
    
    }
    

    Now to store String Value: use below statement in your fragment

    SharedPrefrenceUtils.putString(mContext,"username", "1234");
    SharedPrefrenceUtils.putString(mContext,"email", "xyz@gmail.com");
    

    To get String Value: use below statement in your fragment

    String userName = SharedPrefrenceUtils.getString(mContext,"username");
    String email = SharedPrefrenceUtils.getString(mContext,"email");
    

    Same way you can store and retrieve any other value from any fragment or activity of your application.

    0 讨论(0)
提交回复
热议问题