Tuesday, April 24, 2012

Android - assign and retrieve ID's dynamically

I know how to assing ID's dynamically by invoking setID(). For the ID's to be unique, I used to utilize ids.xml and pass to setID() ID's from the pre-generated pool of ID's.



Question 1: Is there any way to assign ID's without utilizing the ids.xml since I cannot participate how many ID's I will need in runtime?



I tried to by pass the first issue presented in Question 1 by dynamically assigning each of which an id based on its label's hash (each label is unique), but there is no way to gaurantee that ID's won't be colliding with ID's auto generated in R.java.



Question 1.1: How the ID naming collision can be resolved?



Question 2: Assume I have the ID value of which I assign and generate dynamically. Since the aformentioned ID does not appear in R.id, findViewById() won't be applicable for retrieving the view. Hence, how can the view be retrieved when the ID is known?





No comments:

Post a Comment