How can I save the state of the checkboxes in a custom listview using sharedpreferences?

北慕城南 提交于 2021-02-11 17:24:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9332454/how-can-i-save-the-state-of-the-checkboxes-in-a-custom-listview-using-sharedpref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!