• 存储数组数据到SharedPreferences


    参考自长城Great的博客: http://blog.csdn.net/u011494050/article/details/38851369

    如果要数组数据(如boolean[] 、int[]等)到SharedPreferences时,我们可以先将数组数据组织成json数据存储到SharedPreferences,读取时则对json数据进行解析就ok了。

    保存boolean[] 数组数据:

     1 public static void saveApkEnalbleArray(Context context,boolean[] booleanArray) {  
     2     SharedPreferences prefs = context.getSharedPreferences(APK_ENABLE_ARRAY, Context.MODE_PRIVATE);  
     3     JSONArray jsonArray = new JSONArray();  
     4     for (boolean b : booleanArray) {  
     5         jsonArray.put(b);  
     6     }  
     7     SharedPreferences.Editor editor = prefs.edit();  
     8     editor.putString(APK_ENABLE_ARRAY,jsonArray.toString());  
     9     editor.commit();  
    10 } 

    读取数据:

     1     public static boolean[] getApkEnableArray(Context context,int arrayLength)
     2     {
     3         SharedPreferences prefs = context.getSharedPreferences(APK_ENABLE_ARRAY, Context.MODE_PRIVATE);
     4         boolean[] resArray=new boolean[arrayLength];
     5         Arrays.fill(resArray, true);
     6         try {
     7             JSONArray jsonArray = new JSONArray(prefs.getString(APK_ENABLE_ARRAY, "[]"));
     8             for (int i = 0; i < jsonArray.length(); i++) {
     9                 resArray[i] = jsonArray.getBoolean(i);
    10             }
    11         } catch (Exception e) {
    12             e.printStackTrace();
    13         }
    14         return resArray;
    15     }
    以上内容仅代表个人理解,如有不适之处,还望不吝赐教!
  • 相关阅读:
    Oracle 常用的十大 DDL 对象
    Oracle DML
    Oracle 的常用概念
    Spring 4 : 整合 SSH
    Spring3 (事务管理)
    Spring2
    Spring 学习笔记一
    Xpath helper下载
    爬取链家北京市二手房的单个房源页信息
    爬取链家北京市二手房的链家编号
  • 原文地址:https://www.cnblogs.com/wojiaowoen/p/7421681.html
Copyright © 2020-2023  润新知