1

I'm making an android game with levels. I made a Button[][] Array which every Button has setEnabled(false) exept the first one which has been set true due to allow players to start the game and "unlock" other levels. I stored a global Boolean Array with the button enabling state so that every time i enter the "LevelsActivity" i can read the Boolean array and update the buttons states. And all of this works fine.

My question is about how to save this Boolean array so that i can load it after the app closing.

I read about SharedPreferences and i find out some code but i can't implement to my purpose. Furthermore i read that array aren't supported by SharedPreferences and i should convert the array to string but i still can't do it. Thanks in advance

This is my Global class if it could help:

public class Globals extends Application {

    private boolean[] array = new boolean[125];

    public Globals() {
        for (int i = 0; i < 125; i++) {
            array[i] = false;
        }
        array[0] = true;
    }

    public boolean getData(int i){
        return this.array[i];
    }

    public void setData(int i, boolean value){
        this.array[i]=value;
    }
}
Ramal
  • 23
  • 5
  • Possible duplicate of [How to add a Boolean Array in Shared preferences in Android](https://stackoverflow.com/questions/27159926/how-to-add-a-boolean-array-in-shared-preferences-in-android) – Vivek Vinodh Oct 29 '19 at 18:34

3 Answers3

0

You can't put a boolean array directly, but you can put a boolean for each level. Something like:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();  
editor.putBoolean("level_one", true);
editor.putBoolean("level_two", false);
...
editor.commit();

and then when you get to the activity where you need to check these values you can get them with

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean level_one = prefs.getBoolean("level_one", false);
Boolean level_two = prefs.getBoolean("level_two", false);
...
JavierCastro
  • 318
  • 2
  • 8
0

I'd store it as a string of 0's and 1's in one SharedPrefs, and then read that String back and populate the boolean array based in that.

 SharedPreferences preferences =
                getApplicationContext().getSharedPreferences("PREFS", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        // Write
        boolean[] array = new boolean[125];
        StringBuilder builder = new StringBuilder();
        for (boolean i : array) {
            builder.append(i ? '1' : '0');
        }


        editor.putString("BOOLEAN_ARRAY", builder.toString()).apply();

        // Read
        String arrayPrefs = preferences.getString("BOOLEAN_ARRAY",null);

        if(!TextUtils.isEmpty(arrayPrefs)){
            for(int i = 0; i < array.length;i++){
                 array[i] = arrayPrefs.charAt(i) == '1';
            }
        }
Jarvis
  • 392
  • 1
  • 6
0

You can use gson library to serialize your data and then save it in sharedPref:

  1. Gson gradle dependency

    implementation 'com.google.code.gson:gson:2.8.6'
    
  2. Serializing array

      val arrayOfBooleans = arrayOf(false, true, false, true)
      // or you could use hashMap which has more readable output after serialization
      val mapOfBooleans = mapOf(1 to false, 5 to true)
    
      val serializedJson1 = Gson().toJson(arrayOfBooleans) // [false,true,false,true]
      val serializedJson2 = Gson().toJson(mapOfBooleans) // {"1":false,"5":true}
    
  3. Saving in SharedPreferences

    fun saveData(json: String) {
    val prefManager =  PreferenceManager.getDefaultSharedPreferences(context)
        prefManager
            .edit()
            .putString(KEY_OF_DATA, json)
            .apply()
    }
    companion object {
        const val KEY_OF_DATA = "keyOfData"
    }
    
Mohammad Sianaki
  • 1,425
  • 12
  • 20