我想在共享首选项中存储一个 bool 数组,我想稍后访问数组元素。任何人都可以帮助我吗?谢谢 advnc。
最佳答案
存储你的数组
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;
}
关于android - 如何在 Android 的共享首选项中添加 bool 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159926/