前言

Android中的SharedPreferences经常使用,这里就对着存储的数据类型进行简单的记录,方便自己回顾。

正文

进入SharedPreferences.java看一下主要的方法。

写入方法

常用的写入数据的方法如下

  1. Editor putStringSet(String key, @Nullable Set<String> values);
  2. Editor putString(String key, @Nullable String value);
  3. Editor putInt(String key, int value);
  4. Editor putLong(String key, long value);
  5. Editor putFloat(String key, float value);
  6. Editor putBoolean(String key, boolean value);
复制

读出方法

常用的读出数据的方法有如下

  1. String getString(String key, @Nullable String defValue);
  2. Set<String> getStringSet(String key, @Nullable Set<String> defValues);
  3. int getInt(String key, int defValue);
  4. long getLong(String key, long defValue);
  5. float getFloat(String key, float defValue);
  6. boolean getBoolean(String key, boolean defValue);
复制

保存List数据

SharedPreferences还可以存储List数据(Collection下的ArrayList,HashSet等),不过类型限定死了,只能是String类型。

使用的方法

  1. Editor putStringSet(String key, @Nullable Set<String> values);
  2. Set<String> getStringSet(String key, @Nullable Set<String> defValues);
复制
存储String类型的List数据

只能是String类型的

写入
  1. ArrayList<String> list = new ArrayList<>();
  2. list.add("张三");
  3. list.add("李四");
  4. list.add("王五");
  5. SharedPreferences sharedPreferences = getSharedPreferences("PREFS_LIST", Context.MODE_PRIVATE);
  6. SharedPreferences.Editor editor = sharedPreferences.edit();
  7. editor.putStringSet("KEY_LIST", new HashSet<>(list));
  8. editor.apply();
复制

存储的数据

  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
  2. <map>
  3.   <set name="KEY_LIST">
  4.       <string>李四</string>
  5.       <string>张三</string>
  6.       <string>王五</string>
  7.   </set>
  8. </map>
复制
读出
  1. ArrayList<String> list = new ArrayList<>();
  2. SharedPreferences sharedPreferences = getSharedPreferences("PREFS_LIST", Context.MODE_PRIVATE);
  3. Set<String> set = sharedPreferences.getStringSet("KEY_LIST", new HashSet<>());
  4. list.addAll(set);
复制
存储自定义的类List数据

如果是自定义类呢,比如Student类

  1. public class Student {
  2.   private final int age;
  3.   private final String name;
  4.   private final float score;
  5.   public Student(int age, String name, float score) {
  6.       this.age = age;
  7.       this.name = name;
  8.       this.score = score;
  9.   }
  10.   public String getName() {
  11.       return name;
  12.   }
  13.   public int getAge() {
  14.       return age;
  15.   }
  16.   public float getScore() {
  17.       return score;
  18.   }
  19.   @NonNull
  20.   @Override
  21.   public String toString() {
  22.       return "Student{" +
  23.               "age=" + age +
  24.               ", name='" + name + '\'' +
  25.               ", score=" + score +
  26.               '}';
  27.   }
  28. }
复制
  1. private ArrayList<Student> mList = new ArrayList<>();
  2. //添加数据
  3. mList.add(new Student(16, "张三", 500));
  4. mList.add(new Student(17, "李四", 490));
  5. mList.add(new Student(15, "王五", 580));
复制

此时如果SharedPreferences要存储mList时,直接使用putStringSet()时不可的。

下面介绍使用方式,需要使用Json存储。

这里使用Gson进行对数据转换

  1. //https://github.com/google/gson
  2. implementation 'com.google.code.gson:gson:2.11.0'
复制

初始化Gson

  1. mGson = new GsonBuilder().setPrettyPrinting().create();
复制
写入
  1. SharedPreferences sharedPref = getSharedPreferences("PREFS_LIST", Context.MODE_PRIVATE);
  2. SharedPreferences.Editor editor = sharedPref.edit();
  3. //mGson把list进行转为String
  4. editor.putString("KEY_LIST", mGson.toJson(mList));
  5. editor.apply();
复制

存储的数据

  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
  2. <map>
  3. <string name="KEY_LIST">[&#10; {&#10; &quot;age&quot;: 16,&#10; &quot;name&quot;: &quot;张三&quot;,&#10; &quot;score&quot;: 500.0&#10; },&#10; {&#10; &quot;age&quot;: 17,&#10; &quot;name&quot;: &quot;李四&quot;,&#10; &quot;score&quot;: 490.0&#10; },&#10; {&#10; &quot;age&quot;: 15,&#10; &quot;name&quot;: &quot;王五&quot;,&#10; &quot;score&quot;: 580.0&#10; }&#10;]</string>
  4. </map>
复制
读出
  1. SharedPreferences sharedPref = getSharedPreferences("PREFS_LIST", Context.MODE_PRIVATE);
  2. String data = sharedPref.getString("KEY_LIST", null);
  3. //通过gson进行转换数据
  4. ArrayList<Student> list = mGson.fromJson(data, new TypeToken<ArrayList<Student>>() {}.getType());
复制

参考文章

相关文章

暂无评论

none
暂无评论...