Problem with saving the situation of check boxes!

Hello everyone
I am trying to manage over 100 checkboxes in my app without creating individual blocks for each one. I’m using the “Any Component” blocks along with a Global List and TinyDB to save and restore the checked states

Here is my current logic (as shown in the attached image):

  1. Saving: When Any Checkbox.Changed, I check if the component is checked. If true, I add the component to a global list. If false, I remove it. Then I store the entire list in TinyDB using the tag “one”.
  2. Loading: On Screen.Initialize, I retrieve the list from TinyDB and use a for each item in list loop to set the Checked property of each item to true.

The Problems I’m facing:

  • The checked states are not being saved or restored correctly.
  • I sometimes get a Runtime Error: “Property setter was expecting a CheckBox component but got a String”.
  • It seems like TinyDB might be converting my component objects into strings when saving, which breaks the set Checkbox.Checked block during initialization.

What is the best way to fix this? Should I store the component’s name/text instead of the component object itself? If so, how can I efficiently find and check the boxes again during Screen Initialize?

also when I open the Story page on my phone I face another error too

Any help or a corrected blocks screenshot would be greatly appreciated!

Before saying anything i would like to know what value is looped here.

When the app opens, all the checkboxes are empty I meant that when it opens it should automatically check the ones that were previously selected and read the items based on those.

yes it will happen because the name of the check box will change eveytime when the screen is initialised. As per your block it seems if the user selectes ny check boxe and later when he open the checked box should be marked, again if he uncheck and open again it have to behave the same. To achieve this simple use index based method like this

understand?

Since the names of the checkboxes are generated dynamically you cannot set the status of saved checkboxes all the time from the tinydb into exisitng checkboxes.

2 Likes

Still you missed what i asked :smiling_face:

Property setter was expecting a com.google.appinventor.components.runtime.CheckBox component but got a String instead.

the problme is , while he selecting a checkbox he get the componet as com.google.appinventor.components.runtime.CheckBox@72555fb but next time when he initialise the screen he get the same checkbox name as com.google.appinventor.components.runtime.CheckBox@35de45. But in the for each item list component was taken the saved component name as String and so he got that error

1 Like

I am going to test your method in photo, I hope that it work for me, I am so tired because it’s long time that I am facing this problem​:pensive_face:

1 Like

Now I face this error

and also now blocks are like this

please tell me what’s the problem one more thing i have 100 check boxes here as a test i choosed 10 of them.

:scream:
Maybe it would be better to create them dynamically

Edit:
Try this, it’s similar to what Still-learning suggested but not identical.
I also recommend using CheckBox.Click because the Changed event is triggered even when the screen is initialized, causing the data to be saved again unnecessarily.


Keep in mind that the number of elements in the list must be the same, otherwise it causes issues with index selection.

However, I strongly recommend generating these elements dynamically, either with DynamicComponents or RecyclerList.

I really like to use Dynamic list or Recycle list but I don’t how to work with them is there any video that teach about these two components?

Ok this time I will use this newvmethod.

Well, there are really many guides in the community,

DynamicComponents

RecyclerList

(There is a simple guide at the end of the topic)

A piece of advice would be to first learn how to use DC and then move on to RL, as it’s a bit more complex.

The choice is yours :grin:
Anyway, if you need anything, the community is always at your disposal

Happy studying, dear

1 Like