I am developing an Activity with a fixed and a dynamic layout part. The dynamic part (and thus the number and type of created widgets) is based on database values this way: For every database row, I will have a group of widgets. And the components of each group depend on the object type (determined from one of the columns) of that database row.
For saving / restoring instance state, all widgets need to have IDs. I would like to ensure that my own IDs will never conflict with IDs that from the generated R class.
For better handling within the app (finding which widget maps to which field in which data object), a numbering scheme like ID = row * 100 + fieldindex would be helpful. Fieldindex is not exactly a colum number. Every object type should have its own fieldindices.
As the ID values in generated R seem not to have consecutive numbers, I can not just predefine a pool of IDs in R and used these without the need of an additional mapping. Second reason against a predefined pool: The big number of fieldindices would result in a large ID pool. Third, the size of the ID pool would limit the row count that can be displayed.
Is there a safe number range I can use for my own layout component's IDs ? Looking at R.java in the gen/ folder, I have the impression that generated IDs are all greater than 0x7f000000. Is that guaranteed ? And if yes, is the range from 0x00000000 up to 0x7f000000 free to use or is it reserved for android's internal purposes ?
Why do you want to avoid the same id? Only thing that necessary is:
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching
. So, you can use your formula
ID = row * 100 + fieldindex
I will work fine, even if it is not unique in project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With