使用SharedPreference保存list数据,其实可以保存Object对象

  • 一个要点,就是使用它储存的对象,必须 implement Serializable,否则会报错
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * SharedPreferences工具类,可以保存object对象
 * <p>
 * 存储时以object存储到本地,获取时返回的也是object对象,需要自己进行强制转换
 * <p>
 * 也就是说,存的人和取的人要是同一个人才知道取出来的东西到底是个啥 ^_^
 */
public class SharedPreferenceUtil {

    /**
     * writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
     * 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
     *
     * @param object 待加密的转换为String的对象
     * @return String   加密后的String
     */
    private static String Object2String(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            objectOutputStream.close();
            return string;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用Base64解密String,返回Object对象
     *
     * @param objectString 待解密的String
     * @return object      解密后的object
     */
    private static Object String2Object(String objectString) {
        byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object object = objectInputStream.readObject();
            objectInputStream.close();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 使用SharedPreference保存对象
     *
     * @param fileKey    储存文件的key
     * @param key        储存对象的key
     * @param saveObject 储存的对象
     */
    public static void save(String fileKey, String key, Object saveObject) {
        SharedPreferences sharedPreferences = 全局类.getApplicationContext().getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String string = Object2String(saveObject);
        editor.putString(key, string);
        editor.commit();
    }

    /**
     * 获取SharedPreference保存的对象
     *
     * @param fileKey 储存文件的key
     * @param key     储存对象的key
     * @return object 返回根据key得到的对象
     */
    public static Object get(String fileKey, String key) {
        SharedPreferences sharedPreferences =  全局类.getApplicationContext().getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
        String string = sharedPreferences.getString(key, null);
        if (string != null) {
            Object object = String2Object(string);
            return object;
        } else {
            return null;
        }
    }
}
  • 待保存的Object对象(实现 Serializable )

public class ClassTest implements Serializable {

    public String mAppId;

    public int mPlatForm;

    public List<ClassInner> mList;

    public static class ClassInner implements Serializable{
        public int id;
        public List<ClassInnerOther> mInnerList;

    }

    public static class ClassInnerOther implements Serializable{
        public String name;
        public int value;
    }
}
  • 使用SP工具类保存内容
ClassTest testClass = new ClassTest();
SharedPreferenceUtil.save("file_key","value_key",testClass );
  • 使用SP工具类获取内容
Object object = SharedPreferenceUtil.get("file_key","value_key");
if(object != null) {
      ClassTest testClass = (ClassTest) object;
}
  • 欢迎交流~
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,803评论 18 399
  • (一)Java部分 1、列举出JAVA中6个比较常用的包【天威诚信面试题】 【参考答案】 java.lang;ja...
    独云阅读 7,146评论 0 62
  • 面向对象主要针对面向过程。 面向过程的基本单元是函数。 什么是对象:EVERYTHING IS OBJECT(万物...
    sinpi阅读 1,100评论 0 4
  • 今天因为一件事,内疚了一段时间。做错了,上午的时候跑去给别人道歉。还好别人也谅解。这件事就这样过去了。 反思自己,...
    楠苜阅读 422评论 1 0
  • 我认为培养见识的旅行比坐在房间里看图的早教更有意义。当然也和小M有个爱游走的妈妈妈妈息息相关。我们总是穿梭在城市间...
    松鼠的日子阅读 290评论 0 1