I have checkboxes in my custom listview, I'm using a boolean array to save the state of these checkboxes. I want to make the state of checkboxes persistent through out the lifetime of the app.I know that this can be achieved through sharedpreferences, but I don't exactly know how this can be done.
Asked
Active
Viewed 293 times
1 Answers
0
I know that this can be achieved through sharedpreferences, but I don't exactly know how this can be done.
There is no option to push serializable objects into sharedpreferences. Because of that, you'll be forced to convert the boolean array to one of the supported types. The only one I can see making sense would be to convert the state of the array into a string like :
"0|1|0|1|1"
Then push that into the shared preferences. To do this you could use the Arrays.toString(boolean []). You will, however, have to write a parse method for extracting the value back out from the SharedPreferences. That is probably the easiest option to accomplish this.

Nick Campion
- 10,479
- 3
- 44
- 58
-
Can u guide me through the whole process? I've never had an experience of working with sharedprefrences. – Dinesh Feb 17 '12 at 17:21
-
There are examples on Google. The only unique thing about your situation is putting in an array of values. – Nick Campion Feb 17 '12 at 17:25
-
yea I get that but read somewhere on stack overflow that each checkbox's state can be saved individually using the putBoolean method of sharedpreferences and later retrieved. What are your thoughts on that idea? – Dinesh Feb 17 '12 at 17:27
-
Seems very cumbersome, you'd have to iterate over your array and create a shared preference for each position. Then on retrieval you'd have to do that same thing. It sounds like more work for less functionality. For instance, it would be kind of annoying to clean up and do other operations. – Nick Campion Feb 17 '12 at 17:29
-
How can I parse a boolean string? – Dinesh Feb 17 '12 at 17:33