Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android的共享首选项中添加布尔数组_Android_Android Activity - Fatal编程技术网

如何在Android的共享首选项中添加布尔数组

如何在Android的共享首选项中添加布尔数组,android,android-activity,Android,Android Activity,我想在共享首选项中存储一个布尔数组,我想稍后访问数组元素。有人能帮我吗?谢谢你的帮助。存储你的阵列 public boolean storeArray(Boolean[] array, String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0); SharedPreferences.Editor ed

我想在共享首选项中存储一个布尔数组,我想稍后访问数组元素。有人能帮我吗?谢谢你的帮助。

存储你的阵列

public boolean storeArray(Boolean[] array, String arrayName, Context mContext) {   

    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayName +"_size", array.length);  

    for(int i=0;i<array.length;i++)  
        editor.putBoolean(arrayName + "_" + i, array[i]); 

    return editor.commit();  
}
public Boolean[] loadArray(String arrayName, Context mContext) {  

    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
    int size = prefs.getInt(arrayName + "_size", 0);  
    Boolean array[] = new Boolean[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getBoolean(arrayName + "_" + i, false);  

    return array;  
}
public boolean storeArray(boolean[]数组,String arrayName,Context mContext){
SharedReferences prefs=mContext.getSharedReferences(“preferencename”,0);
SharedReferences.Editor=prefs.edit();
editor.putInt(arrayName+“_size”,array.length);

对于(int i=0;i全局存储数组设置复选框值

 public boolean setCheckboxarray(Context mContext,Boolean[] array) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(CHECKBOXARRAY, array.length);

    for(int i=0;i<array.length;i++)
        editor.putBoolean(CHECKBOXARRAY + i, array[i]);

    return editor.commit();
}
public Boolean[] getCheckboxarray(Context mContext) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    int size = prefs.getInt(CHECKBOXARRAY, 0);
    Boolean array[] = new Boolean[size];
    for(int i=0;i<size;i++)
        array[i] = prefs.getBoolean(CHECKBOXARRAY+ i, false);

    return array;
}
public boolean setCheckboxarray(上下文mContext,boolean[]数组){
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(mContext);
SharedReferences.Editor=prefs.edit();
putInt(CHECKBOXARRAY,array.length);

对于(int i=0;i使用复选框在SharedReferences中全局存储ArrayList)

 public boolean saveCheckboxarray(Context mContext, ArrayList<Boolean> array) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(CHECKBOXARRAY, array.size());

    for(int i=0;i<array.size();i++)
        editor.putBoolean(CHECKBOXARRAY + i,array.get(i));

    return editor.commit();
}
public boolean saveCheckboxarray(上下文mContext,ArrayList数组){
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(mContext);
SharedReferences.Editor=prefs.edit();
editor.putInt(CHECKBOXARRAY,array.size());

对于(int i=0;我还有另一个问题:
public ArrayList<Boolean> getCheckboxarray(Context mContext) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    int size = prefs.getInt(CHECKBOXARRAY, 0);
    ArrayList<Boolean> getArray=new ArrayList<Boolean>();
    for(int i=0;i<size;i++)
        getArray.add(i,prefs.getBoolean(CHECKBOXARRAY + i, false));

    return getArray;
}