Falmarri
Falmarri

Reputation: 48577

Android store array in preferences

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?

Upvotes: 3

Views: 4263

Answers (2)

a54studio
a54studio

Reputation: 975

 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();

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006779

Only if you turn the array into a string.

Upvotes: 2

Related Questions