将LinkedHashMap变量保存到android中的共享首选项

将LinkedHashMap变量保存到android中的共享首选项,android,sharedpreferences,Android,Sharedpreferences,我遇到了一个问题,我想在共享首选项中保存linkedhashmap变量,我的代码在这里 public static LinkedHashMap<String, AppVariable> GlobalAppVariables; if (Constants.DEVICE_ID == 0) { String accounts_service = serverSync.getCentralServer(this,serial_number); if (accounts_service

我遇到了一个问题,我想在共享首选项中保存linkedhashmap变量,我的代码在这里

public static LinkedHashMap<String, AppVariable> GlobalAppVariables;

if (Constants.DEVICE_ID == 0) {
String accounts_service = serverSync.getCentralServer(this,serial_number);
   if (accounts_service != null){
        Constants.MACHINE_CENTRAL_SERVER = accounts_service;
        }
       Constants.REGISTER_DEVICE_SERVICE = String.format("%sCommon/RegisterMachine", Constants.MACHINE_CENTRAL_SERVER); 
       GlobalAppVariables = serverSync.registerDevice(this, serial_number);       
} 

        SharedPreferences sharedPref= getSharedPreferences("mypref", 0);    
    SharedPreferences.Editor editor= sharedPref.edit();
    editor.putString("name", GlobalAppVariables.toString());
    editor.commit();     

    SharedPreferences sharedPref1= getSharedPreferences("mypref", 0);
    LinkedHashMap<String, AppVariable> reqVariables= sharedPref1.getString("name", "");
当我像上面那样构建代码时,我就遇到了问题

The method putString(String, String) in the type SharedPreferences.Editor is not applicable for the arguments (String, LinkedHashMap<String,AppVariable>)   
类型SharedReferences.Editor中的方法putString(String,String)不适用于参数(String,LinkedHashMap)

因此,请建议我如何保存appvariables。希望这个问题是可以理解的。提前感谢。

您无法将HashMap保存到android中的SharedPreferences。 因此,我认为您可以通过一个技巧来实现这一点,例如:将hashmap的所有键保存为String[]&将所有值保存为String[]

将您的映射放入列表:

例如:

LinkedHashMap < String, String > map
List < String > keyList;
List < String > valueList;

map.keySet();
map.values();
List<String> keyList = new ArrayList<String>(map.keySet());
List<String> valueList = new ArrayList<String>(map.values());
然后将数组保存到您的SharedReferences中

保存阵列:

public boolean saveArray(String[] array, String arrayName, Context mContext) {   
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  
    for(int i=0;i<array.length;i++)  
        editor.putString(arrayName + "_" + i, array[i]);  
    return editor.commit();  
} 
public boolean saveArray(String[]array,String arrayName,Context mContext){
SharedReferences prefs=mContext.getSharedReferences(“preferencename”,0);
SharedReferences.Editor=prefs.edit();
editor.putInt(arrayName+“_size”,array.length);
对于(int i=0;i
//尝试这种方法,希望这将帮助您解决您的问题。。。
HashMap=newHashMap();
地图放置(“名称”、“ANC”);
地图放置(“地址”,“XYZ”);
地图放置(“年龄”、“20”);
地图放置(“电话”、“123456”);
//设置SharedReferences
SharedReferences SharedReferences=GetSharedReferences(“mypref”,0);
字符串键=”;
迭代器itr=map.keySet().Iterator();
while(itr.hasNext()){
String key=itr.next();
SharedPreferences.Editor=sharedPref.edit();
putString(key,map.get(key));
commit();
keys+=itr.next()+“,”;
}
keys=keys.substring(0,keys.length()-1);
SharedPreferences.Editor=sharedPref.edit();
编辑器.putString(“键”,键);
commit();
//获取共享引用
SharedReferences SharedReferences=GetSharedReferences(“mypref”,0);
String[]keysArray=sharedPref.getString(“keys”,“”)。拆分(,“”);

对于(inti=0;i,如果不想编写太多代码,只需下载即可。 这是草图:

private Type entityType = new TypeToken<LinkedHashMap<String, AppVariable>>(){}.getType();

private void save(LinkedHashMap<String, AppVariable> map){
    Gson gson = new Gson();
    String data = gson.toJson(map, entityType);

    getSharedPreferences("mypref", 0)
    .edit()
    .putString("name", data)
    .commit();
}

private LinkedHashMap<String, AppVariable> load(){
    Gson gson = new Gson();
    String data = getSharedPreferences("mypref", 0).getString("name", "");

    return gson.fromJson(data, entityType);
}
private-Type entityType=new-TypeToken(){}.getType();
私有void保存(LinkedHashMap映射){
Gson Gson=新的Gson();
String data=gson.toJson(映射,entityType);
GetSharedReferences(“mypref”,0)
.edit()
.putString(“名称”,数据)
.commit();
}
私有LinkedHashMap加载(){
Gson Gson=新的Gson();
字符串数据=GetSharedReferences(“mypref”,0);
返回gson.fromJson(数据,entityType);
}

您只能在SharedReferences中保存基本数据类型,如
int
float
等。请看一下我在这里没有涉及的内容,从您的列表中获取数组:Foo[]array=list.toArray(new Foo[list.size());这里的Foo[]和list代表overe是什么..如果您想使用“saveArray”函数u应该将数组作为参数传递。因此我将列表转换为数组。行:Foo[]array=list.toArray(new Foo[list.size());为了更简单起见,我做了更改,请选中“立即编辑”。u将两个字符串表示为HashMap=new HashMap();但这里我使用的是公共静态LinkedHashMap GlobalAppVariables;即一个字符串和一个变量。我在editor.putString(key,GlobalAppVariables.get(key));什么是AppVariable?AppVariable是一个包含整数值和字符串值的类
public boolean saveArray(String[] array, String arrayName, Context mContext) {   
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  
    for(int i=0;i<array.length;i++)  
        editor.putString(arrayName + "_" + i, array[i]);  
    return editor.commit();  
} 
public String[] loadArray(String arrayName, Context mContext) {  
    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    String array[] = new String[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getString(arrayName + "_" + i, null);  
    return array;  
}
// Try this way,hope this will help you to solve your problem...

        HashMap<String,String> map = new HashMap<String, String>();
        map.put("name","ANC");
        map.put("address","XYZ");
        map.put("age","20");
        map.put("phone","123456");


        // Set SharedPreferences
        SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
        String keys="";
        Iterator<String> itr = map.keySet().iterator();
        while (itr.hasNext()) {
            String key = itr.next();
            SharedPreferences.Editor editor= sharedPref.edit();
            editor.putString(key,map.get(key));
            editor.commit();
            keys += itr.next()+",";
        }
        keys = keys.substring(0,keys.length()-1);
        SharedPreferences.Editor editor= sharedPref.edit();
        editor.putString("keys",keys);
        editor.commit();

        // Get SharedPreferences
        SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
        String[] keysArray = sharedPref.getString("keys","").split(",");
        for (int i=0;i<keysArray.length;i++){
            System.out.println("Key : "+keysArray[i]+" Value : "+sharedPref.getString(keysArray[i],""));
        }
private Type entityType = new TypeToken<LinkedHashMap<String, AppVariable>>(){}.getType();

private void save(LinkedHashMap<String, AppVariable> map){
    Gson gson = new Gson();
    String data = gson.toJson(map, entityType);

    getSharedPreferences("mypref", 0)
    .edit()
    .putString("name", data)
    .commit();
}

private LinkedHashMap<String, AppVariable> load(){
    Gson gson = new Gson();
    String data = getSharedPreferences("mypref", 0).getString("name", "");

    return gson.fromJson(data, entityType);
}