3

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?

2 Answers 2

2

Only if you turn the array into a string.

Sign up to request clarification or add additional context in comments.

2 Comments

then how to retrieve integer array again while loading array from preference for android 2.1.
turn the string into an array... (one easy way of doing this could be using JSON formatter/parser)
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.