如何存储HashMap中,这样就可以保持设备重启后它的价值? [英] How to store hashmap so that it can be retained it value after a device reboot?

查看:111
本文介绍了如何存储HashMap中,这样就可以保持设备重启后它的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储在HashMap对象中的全局类,以便它甚至会在手机重启后保存价值。不知道如何去了解这个概念。

I want to store the hashmap object in global class so that it will store value even after the mobile restart. Any idea how to go about this concept.

推荐答案

非常感谢,但可以使用共享preferences技术来完成同样的事情。 下面是code添加数据到共享preferences,并检查是否已经存在。

Thanks very much but same thing can be done using the shared Preferences technique. Below is the code to add data into shared preferences and check if already exists.

  SharedPreferences preferences = getSharedPreferences(
                PREF_FILE_NAME, MODE_PRIVATE);
        if (value.equals("")) {

            boolean storedPreference = preferences.contains(key);
            if (storedPreference) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.remove(key); // value to store
                Log.d("KEY",key);
                editor.commit();
            }
        }else{

            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(key, value); // value to store
            Log.d("KEY",key);
            editor.commit();
        }

那么我们可以使用访问

then we can access using the

SharedPreferences preferences = getSharedPreferences(
                PREF_FILE_NAME, MODE_PRIVATE);
        Map<String, String> map = (Map<String, String>) preferences.getAll();
        if(!map.isEmpty()){
            Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
            while(iterator.hasNext()){
                 Map.Entry pairs = (Map.Entry)iterator.next();
                    pairs.getKey()+pairs.getValue();
                          //write code here
            }
        }

这篇关于如何存储HashMap中,这样就可以保持设备重启后它的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆