如何保存和加载布尔数组Android Studio

如何保存和加载布尔数组Android Studio,android,android-studio,android-intent,Android,Android Studio,Android Intent,我正在制作一个有关卡的android游戏。我制作了一个按钮[][]数组,每个按钮都设置为Enabled(false),除了第一个设置为true的按钮,因为它允许玩家启动游戏并“解锁”其他关卡。我存储了一个带有按钮启用状态的全局布尔数组,这样每当我输入“LevelsActivity”时,我就可以读取布尔数组并更新按钮状态。所有这些都很好 我的问题是如何保存这个布尔数组,以便在应用程序关闭后加载它 我阅读了SharedReferences,找到了一些代码,但无法实现我的目的。此外,我了解到Share

我正在制作一个有关卡的android游戏。我制作了一个按钮[][]数组,每个按钮都设置为Enabled(false),除了第一个设置为true的按钮,因为它允许玩家启动游戏并“解锁”其他关卡。我存储了一个带有按钮启用状态的全局布尔数组,这样每当我输入“LevelsActivity”时,我就可以读取布尔数组并更新按钮状态。所有这些都很好

我的问题是如何保存这个布尔数组,以便在应用程序关闭后加载它

我阅读了SharedReferences,找到了一些代码,但无法实现我的目的。此外,我了解到SharedReferences不支持数组,我应该将数组转换为字符串,但我仍然不能这样做。提前谢谢

这是我的全球课程,如果有帮助的话:

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;
    }
}
public类Globals扩展应用程序{
私有布尔[]数组=新布尔[125];
公共全局(){
对于(int i=0;i<125;i++){
数组[i]=false;
}
数组[0]=true;
}
公共布尔getData(int i){
返回此.array[i];
}
公共void setData(int i,布尔值){
this.array[i]=值;
}
}

不能直接放置布尔数组,但可以为每个级别放置布尔值。比如:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();  
editor.putBoolean("level_one", true);
editor.putBoolean("level_two", false);
...
editor.commit();
然后,当您进入需要检查这些值的活动时,您可以使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean level_one = prefs.getBoolean("level_one", false);
Boolean level_two = prefs.getBoolean("level_two", false);
...

不能直接放置布尔数组,但可以为每个级别放置布尔值。比如:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();  
editor.putBoolean("level_one", true);
editor.putBoolean("level_two", false);
...
editor.commit();
然后,当您进入需要检查这些值的活动时,您可以使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean level_one = prefs.getBoolean("level_one", false);
Boolean level_two = prefs.getBoolean("level_two", false);
...

我将它作为一个0和1的字符串存储在一个SharedPref中,然后将该字符串读回并基于该字符串填充布尔数组

 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';
            }
        }
SharedReferences首选项=
getApplicationContext().GetSharedReferences(“PREFS”,Context.MODE\u PRIVATE);
SharedReferences.Editor=首选项.edit();
//写
布尔[]数组=新布尔[125];
StringBuilder=新的StringBuilder();
for(布尔i:数组){
builder.append(i?'1':'0');
}
editor.putString(“布尔数组”,builder.toString()).apply();
//阅读
String arrayPrefs=preferences.getString(“布尔数组”,null);
如果(!TextUtils.isEmpty(arrayPrefs)){
for(int i=0;i
我将它存储为一个由0和1组成的字符串,存储在一个SharedRefs中,然后将该字符串读回并基于该字符串填充布尔数组

 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';
            }
        }
SharedReferences首选项=
getApplicationContext().GetSharedReferences(“PREFS”,Context.MODE\u PRIVATE);
SharedReferences.Editor=首选项.edit();
//写
布尔[]数组=新布尔[125];
StringBuilder=新的StringBuilder();
for(布尔i:数组){
builder.append(i?'1':'0');
}
editor.putString(“布尔数组”,builder.toString()).apply();
//阅读
String arrayPrefs=preferences.getString(“布尔数组”,null);
如果(!TextUtils.isEmpty(arrayPrefs)){
for(int i=0;i
您可以使用gson库序列化数据,然后将其保存在SharedRef中:

  • Gson梯度依赖性

    implementation 'com.google.code.gson:gson:2.8.6'
    
      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}
    
  • 序列化数组

    implementation 'com.google.code.gson:gson:2.8.6'
    
      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}
    
  • 保存在SharedReferences中

    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"
    }
    

  • 您可以使用gson库序列化数据,然后将其保存在SharedRef中:

  • Gson梯度依赖性

    implementation 'com.google.code.gson:gson:2.8.6'
    
      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}
    
  • 序列化数组

    implementation 'com.google.code.gson:gson:2.8.6'
    
      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}
    
  • 保存在SharedReferences中

    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"
    }
    
  • 可能的重复可能的重复