使用SharedPreferences存储List
什么是SharedPreferences
SharedPreferences是Android平台提供的一种轻量级的数据存储方式,用于存储少量的键值对数据。它以XML文件的形式存储在设备的内部存储空间中,可以被多个应用程序共享和访问。
SharedPreferences的特点如下:
简单易用:使用SharedPreferences可以方便地存储和获取数据,无需编写复杂的数据库操作代码。
轻量级:SharedPreferences适用于存储少量的简单数据,不适合存储大量的复杂数据。
高效性能:SharedPreferences的读写操作比数据库操作更快速,适用于需要频繁读写的场景。
存储List到SharedPreferences
在Android开发中,经常会遇到需要将List保存到SharedPreferences的情况。SharedPreferences本身只支持存储基本数据类型,如字符串、整数等,而不支持直接存储List。但我们可以通过一些方法来实现将List存储到SharedPreferences的功能。
方法一:将List转换为JSON字符串存储
一种常见的方法是将List转换为JSON字符串,然后将JSON字符串存储到SharedPreferences中。当需要读取List时,再将JSON字符串转换回List。
首先,我们需要引入Gson库,它是Google提供的用于在Java对象和JSON数据之间进行转换的库。在adle文件的dependencies中添加以下依赖:
implementation 'de.gson:gson:2.8.7'
接下来,我们可以定义一个工具类来实现List和JSON字符串之间的转换:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
public class ListUtils {
    private static Gson gson = new Gson();
    public static <T> String listToJson(List<T> list) {
        return gson.toJson(list);
    }
    public static <T> List<T> jsonToList(String json) {
        Type type = new TypeToken<List<T>>() {}.getType();
        return gson.fromJson(json, type);
    }
}
以上工具类中的listToJson方法将List转换为JSON字符串,而jsonToList方法将JSON字符串转换为List。
接下来,我们可以使用SharedPreferences存储List。假设我们要存储一个字符串类型的List,可以按照以下步骤进行操作:
// 获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
// 将List转换为JSON字符串
List<String> stringList = new ArrayList<>();
stringList.add("item1");
stringList.add("item2");
stringList.add("item3");
String json = ListUtils.listToJson(stringList);
// 将JSON字符串存储到SharedPreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("list_key", json);
editor.apply();
以上代码中,我们通过SharedPreferences的edit方法获取到Editor对象,然后使用putString方法将JSON字符串存储到SharedPreferences中,并通过apply方法提交保存操作。
方法二:将List转换为逗号分隔的字符串存储
另一种方法是将List转换为逗号分隔的字符串,然后将字符串存储到SharedPreferences中。当需要读取List时,再将字符串按逗号分隔转换回List。
我们可以定义一个工具类来实现List和字符串之间的转换:
public class ListUtils {
    public static String listToString(List<String> list) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String item : list) {
            stringBuilder.append(item).append(",");
        }
        if (stringBuilder.length() > 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }
    public static List<String> stringToList(String string) {
        return Arrays.asList(string.split(","));
    }
}
以上工具类中的listToString方法将List转换为逗号分隔的字符串,而stringToList方法将逗号分隔的字符串转换为List。
使用SharedPreferences存储List的代码示例:
// 获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
/
/ 将List转换为逗号分隔的字符串
List<String> stringList = new ArrayList<>();
stringList.add("item1");
stringList.add("item2");
stringList.add("item3");
String string = ListUtils.listToString(stringList);editor记忆方法
// 将字符串存储到SharedPreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("list_key", string);
editor.apply();
以上代码中,我们通过SharedPreferences的edit方法获取到Editor对象,然后使用putString方法将字符串存储到SharedPreferences中,并通过apply方法提交保存操作。
读取SharedPreferences中的List
当需要读取SharedPreferences中的List时,我们可以按照以下步骤进行操作。
方法一:从JSON字符串中读取List
假设我们之前将List以JSON字符串的形式存储到SharedPreferences中,现在需要读取它:
// 获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
// 从SharedPreferences中读取JSON字符串
String json = sharedPreferences.getString("list_key", "");
/
/ 将JSON字符串转换为List
List<String> stringList = ListUtils.jsonToList(json);
以上代码中,我们通过SharedPreferences的getString方法获取到之前存储的JSON字符串,然后通过ListUtils工具类的jsonToList方法将JSON字符串转换为List。
方法二:从逗号分隔的字符串中读取List
假设我们之前将List以逗号分隔的字符串的形式存储到SharedPreferences中,现在需要读取它:
// 获取SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
// 从SharedPreferences中读取逗号分隔的字符串
String string = sharedPreferences.getString("list_key", "");
// 将逗号分隔的字符串转换为List
List<String> stringList = ListUtils.stringToList(string);
以上代码中,我们通过SharedPreferences的getString方法获取到之前存储的逗号分隔的字符串,然后通过ListUtils工具类的stringToList方法将逗号分隔的字符串转换为List。