数据本地存储为对象

  • 内容
  • 相关

一、作用

    很多情况会用到数据存储,比如Student对象,有很多以这个对象为核心的数据。

二、使用

2.1写一个Config

public class SchooltConfig {
    private StudentConfig student1;
    private StudentConfig student2;
    private TeacherConfig teacher1;
    private TeacherConfig teacher2;
    //set  get

    public SchooltConfig() {
        student1 = new StudentConfig();
        student2 = new StudentConfig();
        teacher1 = new TeacherConfig();
        teacher2 = new TeacherConfig();
    }

    public static class StudentConfig {
        int id
        String name;
        int age;
        // set get  
        ......
    }

    public static class TeacherConfig{
        int id
        String name;
        int age;
        // set get  
        ......
    }
}

2.2存

2.2.1数据存的使用
private SchoolConfig.StudentConfig student1Config; 
private SchoolConfig.StudentConfig student2Config; 
student1Config= ConfigKeeper.Inst().schoolConfig.getStudent1Config;
student2Config= ConfigKeeper.Inst().schoolConfig.getStudent2Config;

student1Config.setId = 1;
student1Config.setName = "张三";
student1Config.setAge = 16;
student2Config.setId = 2;
student2Config.setName = "李四";
student2Config.setAge = 18;
//在onDestroy()或者其他地方存
ConfigKeeper.Inst().saveSchoolConfig();
2.2.2写一个ConfigKeeper.java(其他工具类util见最后)
public class ConfigKeeper {
    private String TAG = this.getClass().getName();
    private SchoolConfig schoolConfig = new SchoolConfig();
    private File schoolFile = null;
    private static ConfigKeeper inst = null;
    private boolean initialized = false;

    private ConfigKeeper() {
        initConfig();
    }

    public static ConfigKeeper Inst() {
        if (inst == null) {
            inst = new ConfigKeeper();
        }
        return inst;
    }

    public void initConfig() {
        synchronized (this) {
            if (!initialized) {  
                schoolFile = getSchoolConfigFile();
                if (schoolFile.exists()) {
                    schoolConfig = new Gson().fromJson(IOUtils.readStr(schoolFile), SchoolConfig.class);
                    if (schoolConfig == null) {
                        schoolConfig = new SchoolConfig();
                    }
                }
                initialized = true;
            }
        }
    }

    public void saveSchoolConfig() {
        if (权限判断) {
            Log.e("TAG", "保存配置错误,未获取到写入文件权限")
            return;
        }

        if (schoolFile != null) {
            ThreadPool.getInstance().executeSingle(new Runnable() {
                @Override
                public void run() {
                    if (schoolConfig != null) {
                        String configJson = new Gson().toJson(schoolConfig);
                        IOUtils.writeStr(schoolFile, configJson);
                    }
                }
            });
        }
    }

    public SchoolConfig getSchoolConfig() {
        return schoolConfig;
    }

    public void setSchoolConfig(SchoolConfig conf) {
        schoolConfig = conf;
    }

    private File getSchoolConfigFile() {
        return FileUtils.getFile("config", "school_config.json");
    }
}
2.3取
//student1Config的实例看存
if (student1Config.getName != null) {
    String name = student1Config.getName;
}
....

三、注意

    等我想到在写

四、附链接

    FileUtils、IOUtils、ThreadPool(还没写)

本文标签:

版权声明:若无特殊注明,本文皆为《admin_H》原创,转载请保留文章出处。

本文链接:数据本地存储为对象 - https://blog.bnist.com/post/16

发表评论

电子邮件地址不会被公开。 必填项已用*标注

未显示?请点击刷新

允许邮件通知
Sitemap