分享:
KObjectSave
用来代替android SharedPreferences的键值存储方式, 支持几乎所有类型(int, string ,Date ,bool ,List等),
它可以方便的存储应用的设置选项,用户的点击记录等
在app的build.gradle中添加
dependencies {
compile 'cn.ikaze.KObjectSave:library:1.0.0'
}
首先要有一个基础的类用于存储
public class Student { int no = 1; //default 1 String name = "gojuukaze";//default gojuukaze Date birthday = new Date(); //default now List<String> subjects; public String toString() { Format format = new SimpleDateFormat("yyyy-MM-dd"); String birthdayString = format.format(birthday); return "No." + no + ", name: " + name + ", birthday: " + birthdayString + ", subjects: " + subjects; } }
使用 KObjectSave
存储 Student
KObjectSave kSave = new KObjectSave(this, Student.class); Student st = (Student) kSave.getObj(); st.no=23; st.name="A"; st.subjects=new ArrayList<String>(){{add("math");add("physics");}}; try { kSave.save(st); } catch (IOException | IllegalAccessException e) { e.printStackTrace(); }
加载
Student tempSt = (Student) new KObjectSave(this, Student.class).getObj(); lod.d("KObjectSave",""+tempSt);
数据没有加密,不要用户存储用户密码等隐私数据
发表评论 (对文章评论)