Android实现List<String>存储到SharedPreferences工具类

来源:互联网 发布:淘宝登录页面网页制作 编辑:程序博客网 时间:2024/05/21 00:55

轻量级的数据,需要保存到SharedPreferences中,但只能保存一些整形、Long、布尔型、字符串,类似List<String>存储没有实现,当然你可以选择存储到本地sqlite数据库,考虑到自己的需求来看,如果本身数据较少,比如说用户创建的文件夹名称,用于分类一些图片,用户不可能创建成千上万个名称保存的,所以没必要增加数据库,增加了反而提升了复杂度。

另外android也是可以存储数组到SharedPreferences的,在3.0以上的api已经实现,SharedPreferences提供了putStringSet方法,可以保存字符串数组。看到Set,那我们知道这个是无顺序的,就算你使用LinkedHashSet也无济于事,因为有人看了源码实现,发现底层都是转化为HashSet实现的,所以取出无序自然无法得到保障,除非自己去重写。


为什么需要有序的List<String>,想想一种场景结合ListView、GridView使用,这种有序可以保证用户的先后创建顺序,取出的时候也是有序的,通过展示自然也是有序的。


好了,下面是我的一个工具类,当然可以扩展为一个完整的SharedPreferencesUtil,我这里只是实现了将List<String>存储到SharedPreferences,并可以移除的部分。注释都在代码中了。

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<String>工具类 */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<String> *  * @param context * @param key *            List<String>对应的key * @param strList *            对应需要存储的List<String> */public static void putStrListValue(Context context, String key,List<String> 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<String> *  * @param context * @param key *            List<String> 对应的key * @return List<String> */public static List<String> getStrListValue(Context context, String key) {List<String> strList = new ArrayList<String>();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<String>所有数据 *  * @param context * @param key *            List<String>对应的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<String>单条数据 * @param context * @param key *            List<String>对应的key * @param str *            List<String>中的元素String */public static void removeStrListItem(Context context, String key, String str) {int size = getIntValue(context, key + "size", 0);if (0 == size) {return;}List<String> strList = getStrListValue(context, key);// 待删除的List<String>数据暂存List<String> removeList = new ArrayList<String>();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();}}


1 0
原创粉丝点击