Android实现List存储到SharedPreferences工具类
16lz
2021-01-26
轻量级的数据,需要保存到SharedPreferences中,但只能保存一些整形、Long、布尔型、字符串,类似List
另外android也是可以存储数组到SharedPreferences的,在3.0以上的api已经实现,SharedPreferences提供了putStringSet方法,可以保存字符串数组。看到Set,那我们知道这个是无顺序的,就算你使用LinkedHashSet也无济于事,因为有人看了源码实现,发现底层都是转化为HashSet实现的,所以取出无序自然无法得到保障,除非自己去重写。
为什么需要有序的List
好了,下面是我的一个工具类,当然可以扩展为一个完整的SharedPreferencesUtil,我这里只是实现了将List
import java.util.ArrayList;import java.util.List;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;//import android.util.Log;/** * SharedPrefs保存List工具类 */public class SharedPrefsStrListUtil {/** 数据存储的XML名称 **/public final static String SETTING = "SharedPrefsStrList";/** * 存储数据(Int) * * @param context * @param key * @param value */private static void putIntValue(Context context, String key, int value) {Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();sp.putInt(key, value);sp.commit();}/** * 存储数据(String) * * @param context * @param key * @param value */private static void putStringValue(Context context, String key, String value) {Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();sp.putString(key, value);sp.commit();}/** * 存储List * * @param context * @param key * List对应的key * @param strList * 对应需要存储的List */public static void putStrListValue(Context context, String key,List strList) {if (null == strList) {return;}// 保存之前先清理已经存在的数据,保证数据的唯一性removeStrList(context, key);int size = strList.size();putIntValue(context, key + "size", size);for (int i = 0; i < size; i++) {putStringValue(context, key + i, strList.get(i));}}/** * 取出数据(int) * * @param context * @param key * @param defValue * 默认值 * @return */private static int getIntValue(Context context, String key, int defValue) {SharedPreferences sp = context.getSharedPreferences(SETTING,Context.MODE_PRIVATE);int value = sp.getInt(key, defValue);return value;}/** * 取出数据(String) * * @param context * @param key * @param defValue * 默认值 * @return */private static String getStringValue(Context context, String key,String defValue) {SharedPreferences sp = context.getSharedPreferences(SETTING,Context.MODE_PRIVATE);String value = sp.getString(key, defValue);return value;}/** * 取出List * * @param context * @param key * List 对应的key * @return List */public static List getStrListValue(Context context, String key) {List strList = new ArrayList();int size = getIntValue(context, key + "size", 0);//Log.d("sp", "" + size);for (int i = 0; i < size; i++) {strList.add(getStringValue(context, key + i, null));}return strList;}/** * 清空List所有数据 * * @param context * @param key * List对应的key */public static void removeStrList(Context context, String key) {int size = getIntValue(context, key + "size", 0);if (0 == size) {return;}remove(context, key + "size");for (int i = 0; i < size; i++) {remove(context, key + i);}}/** * @Description TODO 清空List单条数据 * @param context * @param key * List对应的key * @param str * List中的元素String */public static void removeStrListItem(Context context, String key, String str) {int size = getIntValue(context, key + "size", 0);if (0 == size) {return;}List strList = getStrListValue(context, key);// 待删除的List数据暂存List removeList = new ArrayList();for (int i = 0; i < size; i++) {if (str.equals(strList.get(i))) {if (i >= 0 && i < size) {removeList.add(strList.get(i));remove(context, key + i);putIntValue(context, key + "size", size - 1);}}}strList.removeAll(removeList);// 删除元素重新建立索引写入数据putStrListValue(context, key, strList);}/** * 清空对应key数据 * * @param context * @param key */public static void remove(Context context, String key) {Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();sp.remove(key);sp.commit();}/** * 清空所有数据 * * @param context */public static void clear(Context context) {Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();sp.clear();sp.commit();}}
更多相关文章
- Android录音mp3格式实例详解
- plist读写,NSArray,NSData,NSnumber,字典等简使用
- 阿里巴巴 面试经历
- android绘图 报表----aChartEngine图表显示(1)
- Android如何获取系统联系人数据?
- Android之AIDL使用解析
- Android(安卓)SQLite分析
- 第三部分:Android(安卓)应用程序接口指南---第一节:应用程序组件--
- Android(安卓)RxJava:详解 条件 / 布尔操作符