yydl
yydl

Reputation: 24474

Storing Many Booleans in Android

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/retrieve these boolean values?

Upvotes: 0

Views: 228

Answers (2)

Vikas Patidar
Vikas Patidar

Reputation: 43349

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.

Upvotes: 1

West_Link
West_Link

Reputation: 115

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"

Upvotes: 1

Related Questions