Android store array in preferences

隐身守侯 提交于 2019-12-23 17:19:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!