开发者

Storing Many Booleans in Android

开发者 https://www.devze.com 2023-02-17 15:03 出处:网络
I have a known number of checkboxes (e.g. 100). I want to be able to store these so that I can read them at runtime (and repopulate the checkboxes). I would prefer using SharedPreferences for the stor

I have a known number of checkboxes (e.g. 100). I want to be able to store these so that I can read them at runtime (and repopulate the checkboxes). I would prefer using SharedPreferences for the storage.

What is the best way to store/retrie开发者_Python百科ve these boolean values?


If you simply want to save state of those checkboxes in the same activity context then mantain a boolean ArrayList and if you want them again when you run your application next time or something else than SharedPreferences is a better way for this.

I have written a sample code:

SharedPreferences prefs = getSharedPreferences(
                            "CheckBoxStates", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

editor.putBoolean("CheckBox1", mCheckBox1.isChecked());
editor.commit();

As your have many CheckBoxes so simply you can use a loop condition here.


store all boolean values in a string that contain a comma seperated 0-1 sequence, for example: "1,0,1,1," means "true,false,true,true"

0

精彩评论

暂无评论...
验证码 换一张
取 消