1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// HashMap 저장
public void SaveUrlMap(Context context, HashMap<StringString> hashMapData) {
    SharedPreferences mmPref = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
    if (mmPref != null) {
        JSONObject jsonObject = new JSONObject(hashMapData);
        String jsonString = jsonObject.toString();
        SharedPreferences.Editor editor = mmPref.edit();
        editor.remove("hashMapName").commit();
        editor.putString("hashMapName", jsonString);
        editor.commit();
    }
}
 
// HashMap 불러오기
public HashMap<StringString> LoadUrlMap(Context context) {
    HashMap<StringString> outputMap = new HashMap<StringString>();
    SharedPreferences mmPref = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
    try {
        if (mmPref != null) {
            String jsonString = mmPref.getString("hashMapName", (new JSONObject()).toString());
            JSONObject jsonObject = new JSONObject(jsonString);
 
            Iterator<String> keysItr = jsonObject.keys();
            while (keysItr.hasNext()) {
                String key = keysItr.next();
                String value = (String) jsonObject.get(key);
                outputMap.put(key, value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return outputMap;
}
cs

+ Recent posts