问题
I know only primitives can be stored in the android preferences, but do arrays count? Can I store an array of, say, Strings or booleans in an android preference?
回答1:
Only if you turn the array into a string.
回答2:
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
for(int n =0;n<LevelMenu.buttonState.length;n++){
LevelMenu.buttonState[n]= (byte) settings.getInt("levelsave"+n,0);
}
Above will get and populate the array and below will depopulate and save.
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = settings.edit();
for(int n =0;n<LevelMenu.buttonState.length;n++){
editor.putInt("levelsave"+n,LevelMenu.buttonState[n]);
}
editor.commit();
来源:https://stackoverflow.com/questions/3249996/android-store-array-in-preferences