views:

80

answers:

1

I have some 34 checkboxes on one Activity which i have to mark and unmark. and I want to save the status of the each checkbox.

I want this persistently. So should i have a sqlite table or preferences ?

If sqlite table is recommended, then what should be the ideal table structure design?

+1  A: 

You could do it either by using SQLite or preferences. Though... what you want to do is kind of simple, so why would you care of creating a table, creating SQliteOpenHelper objects, designing a table, etc. The preferences approach is simpler.

So... what is most important here is how you identify your checkboxes. If you have just 34 static checkboxes (meaning they won't change its order) and they are into an array (which would be ideal), you have it easy.

As you might know, preferences editor does not allow to save arrays. So you have some choices:

  • Saving each state into a different preference slot (for instance: putBoolean("checkbox2", true); putBoolean("checkbox3", false); ect.)
  • Save all states into an String: putString("checkboxes_state", "10101011100011010"); this way you will have a String with 34 chars, each char represents a state.
  • If you want efficiency, you can represent all states with a simple long equals or lower than 17179869183. For instance putLong("checkboxes_state", 17179869183) would mean that all checkboxes are selected (because its binary representation is 1111111111111111111111111111111111). More examples:
    • 12159999103 = 1011010100110010101101110001111111
    • 1 = 0000000000000000000000000000000001, meaning that only the last checkbox is selected.

Edit

With regards to your question of why the last one is more efficient let me briefly explain. The first one implies using 34 preferences slots (actually I don't know how they are stored; hopefully, android team took care of performance). The second approach will use a 34 chars String; so, the minimum size that the String object would have is:

minimum String memory usage (bytes) = 8 * (int) ((34 * 2 + 45) / 8) = 113 bytes

Meaning that a String of that lengthen will take up at least 113 bytes. On the other hand, using just one long will take up 8 bytes only. The less bytes to work with, the faster your app will be (because Android will have to process and save less bytes).

Cristian
thanks Cristian. Yes I have the static checkboxes. i guess first two approaches you have told are bit easier to implement than the third one.and more over i want to know what makes third one efficient ?
Vijay C
I edited the question to explain that part.
Cristian