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

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


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


好了,下面是我的一个工具类,当然可以扩展为一个完整的SharedPreferencesUtil,我这里只是实现了将List存储到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工具类 */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();}}


更多相关文章

  1. Android录音mp3格式实例详解
  2. plist读写,NSArray,NSData,NSnumber,字典等简使用
  3. 阿里巴巴 面试经历
  4. android绘图 报表----aChartEngine图表显示(1)
  5. Android如何获取系统联系人数据?
  6. Android之AIDL使用解析
  7. Android(安卓)SQLite分析
  8. 第三部分:Android(安卓)应用程序接口指南---第一节:应用程序组件--
  9. Android(安卓)RxJava:详解 条件 / 布尔操作符

随机推荐

  1. Funambol android eclipse上的配置及说明
  2. android 、java优质资料合集
  3. Android(安卓)4.0的图形硬件加速及绘制技
  4. 解决Android Studio Gradle DSL method n
  5. Android访问WebService
  6. Android开发之SlidingDrawer(一)
  7. 使用Android提供的模拟任意地理位置,报jav
  8. Android 中文 API (34) —— RadioGroup
  9. appwidget-provider
  10. android 视频通话 项目 源码 - android大