List存取
来源:互联网 发布:mac电磁阀官网 编辑:程序博客网 时间:2024/05/16 08:59
前言
较小键值集保存应使用SharedPreferences API。适用Int、Long、Float、Boolean、String、StringSet,不适用List。也可选本地sqlite数据库存储,但数据较少则没必要增加数据库以避免复杂度提升。
SharedPreferences中putStringSet()
方法保存Set。但因Set无序且底层转化为HashSet实现,故为求有顺用LinkedHashSet报错:
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.TreeSet
解决
工具类
package util;import android.content.Context;import android.content.SharedPreferences;import java.util.ArrayList;import java.util.List;/** * Created on 2017/11/24. * getSharedPreferences() * Fragment内执行,访通过资源字符串R.string.preference_file_key识别共享首选项文件并用专用模式打开,仅允许您应用访问文件。 * 命名您共享首选项文件应使用对您应用唯一可识别名称,比如"com.example.myapp.PREFERENCE_FILE_KEY"。 * getPreferences() * 仅需Activity一共享首选项,从Activity调此方法,该方法无需提供名称并检索属该Activity默认共享首选项文件。 * * @desc save smaller key sets */public class SharedPrefUtils { /** * 唯一识别XML名称 */ public final static String NAME = "XXX"; /** * Save int data. * * @param context * @param key * @param value */ private static void saveInt(Context context, String key, int value) { SharedPreferences.Editor edit = context.getSharedPreferences(NAME, Context.MODE_PRIVATE).edit(); edit.putInt(key, value); edit.commit(); } /** * Save String data. * * @param context * @param key * @param value */ private static void saveString(Context context, String key, String value) { SharedPreferences.Editor edit = context.getSharedPreferences(NAME, Context.MODE_PRIVATE).edit(); edit.putString(key, value); edit.commit(); } /** * Save List<String> data. * * @param context * @param key * @param list */ public static void saveListString(Context context, String key, List<String> list) { // 保存前清理已存数据以保唯一性 clearListString(context, key); int size = list.size(); saveInt(context, key + "size", size); for (int i = 0; i < size; i++) { saveString(context, key + i, list.get(i)); } } /** * Get int data. * * @param context * @param key * @param defValue * @return */ private static int getInt(Context context, String key, int defValue) { SharedPreferences edit = context.getSharedPreferences(NAME, Context.MODE_PRIVATE); int value = edit.getInt(key, defValue); return value; } /** * Get String data. * * @param context * @param key * @param defValue * @return */ private static String getString(Context context, String key, String defValue) { SharedPreferences edit = context.getSharedPreferences(NAME, Context.MODE_PRIVATE); String value = edit.getString(key, defValue); return value; } /** * Get List<String> data. * * @param context * @param key * @return */ public static List<String> getListString(Context context, String key) { List<String> list = new ArrayList<>(); int size = getInt(context, key + "size", 0); for (int i = 0; i < size; i++) { list.add(getString(context, key + i, null)); } return list; } /** * Clear List<String> data. * * @param context * @param key */ public static void clearListString(Context context, String key) { int size = getInt(context, key + "size", 0); if (0 == size) { return; } clearValueBaseOnKey(context, key + "size"); for (int i = 0; i < size; i++) { clearValueBaseOnKey(context, key + i); } } /** * Clear ListString's one data. * * @param context * @param key * @param str */ public static void clearListStringOne(Context context, String key, String str) { int size = getInt(context, key + "size", 0); if (0 == size) { return; } List<String> list = getListString(context, key); for (String string : list) { if (string.equals(str)) { list.remove(str); saveListString(context, key, list); } } } /** * Clear data corresponding to key. * * @param context * @param key */ public static void clearValueBaseOnKey(Context context, String key) { SharedPreferences.Editor edit = context.getSharedPreferences(NAME, Context.MODE_PRIVATE).edit(); edit.remove(key); edit.commit(); } /** * Clear all data. * * @param context */ public static void clearAll(Context context) { SharedPreferences.Editor edit = context.getSharedPreferences(NAME, Context.MODE_PRIVATE).edit(); edit.clear(); edit.commit(); }}
阅读全文
0 0
- List存取
- List对数据的存取
- list map Iterator 存取值
- python用list存取数组
- Redis存取List对象的实践
- SharedPreferences存取JavaBean对象或List<Bean>
- redis Jedis存取list对象和map
- Redis存取List对象的实践
- List、Map、Set三个接口,存取元素时的特点
- List,Set,Map存取元素各有什么特点?
- SharedPreferences 存List集合,模拟数据库,随时存取
- List、Map、Set三个接口,存取元素的特点
- List、Map、Set三个接口,存取元素时,各有什么特点?
- List、Map、Set三个接口,存取元素时,各有什么特点?
- List、Map、Set三个接口,存取元素时,各有什么特点?
- List、Map、Set三个接口,存取元素时,各有什么特点?
- List、Map、Set三个接口,存取元素时,各自的特点
- List、Map、Set三个接口,存取元素时,各有什么特点?
- 迭代器模式
- 第一行代码--知识点记录
- VisualSVN Server安装失败解决
- Sort小结
- 什么是闭包?
- List存取
- springMVC注解方式的校验
- MyBatis学习笔记--第一天
- 最新阿里云服务器优惠活动汇总
- Codeforces Round #448 (Div. 2) C. Square Subsets
- 轮子哥博客
- 【转载】Ubuntu 安装 OpenVas
- APK重新签名的方法
- docker安装redis