zoukankan      html  css  js  c++  java
  • android存储阵列数据SharedPreferences

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

    比如,我要保存boolean[] 数组数据:

    	public static void saveApkEnalbleArray(Context context,boolean[] booleanArray) {
    		SharedPreferences prefs = context.getSharedPreferences(APK_ENABLE_ARRAY, Context.MODE_PRIVATE);
    		JSONArray jsonArray = new JSONArray();
    		for (boolean b : booleanArray) {
    			jsonArray.put(b);
    		}
    		SharedPreferences.Editor editor = prefs.edit();
    		editor.putString(APK_ENABLE_ARRAY,jsonArray.toString());
    		editor.commit();
    	}


    读取数据:

    	public static boolean[] getApkEnableArray(Context context,int arrayLength)
    	{
    		SharedPreferences prefs = context.getSharedPreferences(APK_ENABLE_ARRAY, Context.MODE_PRIVATE);
    		boolean[] resArray=new boolean[arrayLength]; 
    		Arrays.fill(resArray, true);
    		try {
    		    JSONArray jsonArray = new JSONArray(prefs.getString(APK_ENABLE_ARRAY, "[]"));
    		    for (int i = 0; i < jsonArray.length(); i++) {
    		    	resArray[i] = jsonArray.getBoolean(i);
    		    }
    		} catch (Exception e) {
    		    e.printStackTrace();
    		}
    		
        	        return resArray;
    	}


    当保存一些复杂的对象数组时,能够用gson来处理json和object的相互转换。




  • 相关阅读:
    AD9 如何画4层pcb板
    在Altium Designer 2009下如何添加Logo图
    [置顶] 整数拆分 整合算法
    altium designer 中的top/bottom solder和top/bottom paste mask
    vs2012 与 win7 不兼容的问题
    poj1742 Coins
    poj3181 Dollar Dayz
    poj1065 Wooden Sticks
    poj1631 Bridging signals
    poj3666 Making the Grade
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4558777.html
Copyright © 2011-2022 走看看