I want to store a Boolean array in Shared preferences ,and i want to access the array elements later. Can anybody help me ?.Thanks in advnc.
Asked
Active
Viewed 3,734 times
3
-
1There is another question about this: http://stackoverflow.com/questions/3876680/is-it-possible-to-add-an-array-or-object-to-sharedpreferences-on-android – Trebia Project. Nov 26 '14 at 22:30
3 Answers
8
Store your array
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();
}
Load your array
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;
}

Rami
- 7,879
- 12
- 36
- 66
3
Store your array globally set checkbox value
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();
}
Load your array globally get checkbox value
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;
}

MalhotraUrmil
- 88
- 1
- 10
0
Store ArrayList Globally in sharedpreferences using checkbox.
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();
}
Load ArrayList Globally in sharedpreferences Status.
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;
}

MalhotraUrmil
- 88
- 1
- 10