Java android SharedReferences和Map

Java android SharedReferences和Map,java,android,Java,Android,在我的应用程序中,我使用SharedReferences将一些用户信息(如姓名、体重、身高)保存为键值对 这些信息用于若干活动。因此,我认为,与其在每个活动中实现读/写SharedReference的整个过程,我还可以实现一个类“UserData”,并定义几个静态方法。所以,当我需要一些用户信息或想要保存它们时,我只使用“UserData”类的方法,这个类处理后台的所有内容 所以我做了以下几件事: 类UserData包含私有Map 此映射由SharedReferences 初始化在每个活动的o

在我的应用程序中,我使用SharedReferences将一些用户信息(如姓名、体重、身高)保存为键值对

这些信息用于若干活动。因此,我认为,与其在每个活动中实现读/写SharedReference的整个过程,我还可以实现一个类“UserData”,并定义几个静态方法。所以,当我需要一些用户信息或想要保存它们时,我只使用“UserData”类的方法,这个类处理后台的所有内容

所以我做了以下几件事:

  • UserData
    包含私有
    Map
  • 此映射由
    SharedReferences
  • 初始化在每个活动的
    onCreate()
    -方法中触发
  • 通过
    getValue(String key)
    -方法向定义的键提供值(针对每种可能的类型)
  • 应通过setter方法编写(新)信息
  • 要回写共享首选项,有一个保存功能
但现在我有很多问题:

  • getAll()
    方法 我希望
    getAll()
    将从SharedReferences读取所有键值对。因此,我预计在初始化之后,
    数据
    将包含(字符串,字符串)-对(即“名称”;“最大值”)以及(字符串,整数)-对(即“权重”,85)。我说得对吗

  • 获取值 我如何返回
    getValue(字符串键)
    中的值?如何从这样的
    Map
    Map.Entry
    定义中获取值类型

  • 向地图添加条目 我不知道如何覆盖或写入数据的新条目。一个想法是,为我可以保存在SharedReferences中的每种类型(即String、Integer)创建一个set方法,在这个方法中创建一个条目,然后调用add方法。但这应该是什么样子呢

  • 拯救 这个储蓄功能正常吗?我不是很确定

  • 还是这完全是一种愚蠢的做法? 谢谢你的支持

    这是我的UserData类

    public class UserData {
    
        static private boolean isInit = false;
    
        static private Map<String,?> data = new HashMap<>();
    
        static void initialize(Context context){
            if(UserData.isInit){
                return;
            }
            if(context==null){
                return;
            }
    
            // read data from memory
            SharedPreferences pref = context.getSharedPreferences(context.getString(R.string.app_userdata),Context.MODE_PRIVATE);
            UserData.data = pref.getAll();
            Log.v(TAG,"loaded " + UserData.data.size() +" key-value-pairs into map");
            UserData.isInit=true;
        }
    
        static void reinitialize(Context context){
            UserData.isInit=false;
            UserData.initialize(context);
        }
    
        static <T> T getValue(String key){
            Object value = UserData.data.get(key);
            if(value instanceof T){
                return (T)value;
            }else{
                return null;
            }
        }
    
        static <T> T getValue(String key,T retErr){
            T value = getValue(key);
            if(value!=null){
                return value;
            }else{
                return retErr;
            }
        }
    
        static void setString(String key, String str){
    
        }
    
        static void setInteger(String key, Integer i){
    
        }
    
        static private void addElement(Map.Entry<String,?> element){
        }
    
        static void save(Context context){
            SharedPreferences pref = context.getSharedPreferences(context.getString(R.string.app_userdata),Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
    
           for(Map.Entry<String,?> pair : UserData.data.entrySet()){
               Object value = pair.getValue();
               if(value instanceof String){
                   editor.putString(pair.getKey(),value.toString());
               }else if(value instanceof Integer){
                   editor.putInt(pair.getKey(),(Integer)value);
               }else if(value instanceof Float){
                   editor.putFloat(pair.getKey(),(Float) value);
               }else if(value instanceof Boolean){
                   editor.putBoolean(pair.getKey(),(Boolean) value);
               }
           }
            editor.apply();
        }
    }
    

    我想你是想重新创造轮子。如果您想创建一个用于保存、添加甚至编辑共享pref的自定义类,这很好。Shared pref是一张地图,你可以使用另一张地图,在我看来似乎是倒退了

    SharedPreferences pref = getApplicationContext().getSharedPreferences("sharedPreferences", 0); // 0 - for private mode
    
    将数据存储到共享pref中

    Editor editor = pref.edit();
    editor.putBoolean("name", "bill");
    editor.commit();
    
    得名

    String name = pref.getString("name", null); //value is null if the key 'name' doesnt exist. you can also put in any string value here
    
    从共享pref中删除数据

    editor.remove("name");
    editor.commit();
    
    删除所有内容

    editor.clear();
    editor.commit();
    

    如果您想创建一个这样做的类,那很好,但是不要将数据添加到地图中。

    我认为您正在尝试重新创建轮子。如果您想创建一个用于保存、添加甚至编辑共享pref的自定义类,这很好。Shared pref是一张地图,你可以使用另一张地图,在我看来似乎是倒退了

    SharedPreferences pref = getApplicationContext().getSharedPreferences("sharedPreferences", 0); // 0 - for private mode
    
    将数据存储到共享pref中

    Editor editor = pref.edit();
    editor.putBoolean("name", "bill");
    editor.commit();
    
    得名

    String name = pref.getString("name", null); //value is null if the key 'name' doesnt exist. you can also put in any string value here
    
    从共享pref中删除数据

    editor.remove("name");
    editor.commit();
    
    删除所有内容

    editor.clear();
    editor.commit();
    

    如果您想创建一个这样做的类,那很好,但是不要将数据添加到映射中。

    我的方法背后的想法是节省cpu负载。我有一个活动A,其中用户键入所有数据。在第二个活动B中,我使用数据。使用shardReference,我必须首先保存活动A中的所有内容,然后在打开活动B时直接重新加载。我的方法的优点是,我不需要这么多内存访问。在我的方法中,我只在初始化期间以及明确希望保存所有内容时访问内存。我希望你现在能更好地理解我的想法:D@paul_schaefer再说一次,你现在做的很好。。。但不管怎样,您都希望使用SharedRef提供的功能。不要重新创建轮子。我的方法背后的想法是节省cpu负载。我有一个活动A,其中用户键入所有数据。在第二个活动B中,我使用数据。使用shardReference,我必须首先保存活动A中的所有内容,然后在打开活动B时直接重新加载。我的方法的优点是,我不需要这么多内存访问。在我的方法中,我只在初始化期间以及明确希望保存所有内容时访问内存。我希望你现在能更好地理解我的想法:D@paul_schaefer再说一次,你现在做的很好。。。但不管怎样,您都希望使用SharedRef提供的功能。不要重新创建轮子。