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();    }}