How do you save/store objects in SharedPreferences on Android?

后端 未结 20 2715
野趣味
野趣味 2020-11-22 13:35

I need to get user objects in many places, which contain many fields. After login, I want to save/store these user objects. How can we implement this kind of scenario?

<
20条回答
  •  半阙折子戏
    2020-11-22 14:37

    there are two file solved your all problem about sharedpreferences

    1)AppPersistence.java

        public class AppPersistence {
        public enum keys {
            USER_NAME, USER_ID, USER_NUMBER, USER_EMAIL, USER_ADDRESS, CITY, USER_IMAGE,
            DOB, MRG_Anniversary, COMPANY, USER_TYPE, support_phone
        }
    
        private static AppPersistence mAppPersistance;
        private SharedPreferences sharedPreferences;
    
        public static AppPersistence start(Context context) {
            if (mAppPersistance == null) {
                mAppPersistance = new AppPersistence(context);
            }
            return mAppPersistance;
        }
    
        private AppPersistence(Context context) {
            sharedPreferences = context.getSharedPreferences(context.getString(R.string.prefrence_file_name),
                    Context.MODE_PRIVATE);
        }
    
        public Object get(Enum key) {
            Map all = sharedPreferences.getAll();
            return all.get(key.toString());
        }
    
        void save(Enum key, Object val) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            if (val instanceof Integer) {
                editor.putInt(key.toString(), (Integer) val);
            } else if (val instanceof String) {
                editor.putString(key.toString(), String.valueOf(val));
            } else if (val instanceof Float) {
                editor.putFloat(key.toString(), (Float) val);
            } else if (val instanceof Long) {
                editor.putLong(key.toString(), (Long) val);
            } else if (val instanceof Boolean) {
                editor.putBoolean(key.toString(), (Boolean) val);
            }
            editor.apply();
        }
    
        void remove(Enum key) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(key.toString());
            editor.apply();
        }
    
        public void removeAll() {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.apply();
        }
    }
    

    2)AppPreference.java

    public static void setPreference(Context context, Enum Name, String Value) {
            AppPersistence.start(context).save(Name, Value);
        }
    
        public static String getPreference(Context context, Enum Name) {
            return (String) AppPersistence.start(context).get(Name);
        }
    
        public static void removePreference(Context context, Enum Name) {
            AppPersistence.start(context).remove(Name);
        }
    }
    

    now you can save,remove or get like,

    -save

    AppPreference.setPreference(context, AppPersistence.keys.USER_ID, userID);
    

    -remove

    AppPreference.removePreference(context, AppPersistence.keys.USER_ID);
    

    -get

     AppPreference.getPreference(context, AppPersistence.keys.USER_ID);
    

提交回复
热议问题