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

后端 未结 20 2732
野趣味
野趣味 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:39

    An other way to save and restore an object from android sharedpreferences without using the Json format

    private static ExampleObject getObject(Context c,String db_name){
                SharedPreferences sharedPreferences = c.getSharedPreferences(db_name, Context.MODE_PRIVATE);
                ExampleObject o = new ExampleObject();
                Field[] fields = o.getClass().getFields();
                try {
                    for (Field field : fields) {
                        Class type = field.getType();
                        try {
                            final String name = field.getName();
                            if (type == Character.TYPE || type.equals(String.class)) {
                                field.set(o,sharedPreferences.getString(name, ""));
                            } else if (type.equals(int.class) || type.equals(Short.class))
                                field.setInt(o,sharedPreferences.getInt(name, 0));
                            else if (type.equals(double.class))
                                field.setDouble(o,sharedPreferences.getFloat(name, 0));
                            else if (type.equals(float.class))
                                field.setFloat(o,sharedPreferences.getFloat(name, 0));
                            else if (type.equals(long.class))
                                field.setLong(o,sharedPreferences.getLong(name, 0));
                            else if (type.equals(Boolean.class))
                                field.setBoolean(o,sharedPreferences.getBoolean(name, false));
                            else if (type.equals(UUID.class))
                                field.set(
                                        o,
                                        UUID.fromString(
                                                sharedPreferences.getString(
                                                        name,
                                                        UUID.nameUUIDFromBytes("".getBytes()).toString()
                                                )
                                        )
                                );
    
                        } catch (IllegalAccessException e) {
                            Log.e(StaticConfig.app_name, "IllegalAccessException", e);
                        } catch (IllegalArgumentException e) {
                            Log.e(StaticConfig.app_name, "IllegalArgumentException", e);
                        }
                    }
                } catch (Exception e) {
                    System.out.println("Exception: " + e);
                }
                return o;
            }
            private static void setObject(Context context, Object o, String db_name) {
                Field[] fields = o.getClass().getFields();
                SharedPreferences sp = context.getSharedPreferences(db_name, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                for (Field field : fields) {
                    Class type = field.getType();
                    try {
                        final String name = field.getName();
                        if (type == Character.TYPE || type.equals(String.class)) {
                            Object value = field.get(o);
                            if (value != null)
                                editor.putString(name, value.toString());
                        } else if (type.equals(int.class) || type.equals(Short.class))
                            editor.putInt(name, field.getInt(o));
                        else if (type.equals(double.class))
                            editor.putFloat(name, (float) field.getDouble(o));
                        else if (type.equals(float.class))
                            editor.putFloat(name, field.getFloat(o));
                        else if (type.equals(long.class))
                            editor.putLong(name, field.getLong(o));
                        else if (type.equals(Boolean.class))
                            editor.putBoolean(name, field.getBoolean(o));
                        else if (type.equals(UUID.class))
                            editor.putString(name, field.get(o).toString());
    
                    } catch (IllegalAccessException e) {
                        Log.e(StaticConfig.app_name, "IllegalAccessException", e);
                    } catch (IllegalArgumentException e) {
                        Log.e(StaticConfig.app_name, "IllegalArgumentException", e);
                    }
                }
    
                editor.apply();
            }
    

提交回复
热议问题