Android 2.3.3
I have a table with N rows and N columns. For each row, I should add 4 buttons dynamically, and later do actions based on the button clicked. I know we can set the button IDs with Integer values with button.setID()
, but I want to know whether we can set IDs as string values as we set in XML files, such as btnXYZ1
and btnXYZ2
etc.,
No you cannot set it to
String
, the id isint
value, even when you set it fromXML
it is just the resource name of anint
valueAnd add this listner outside the any method and inside class
The strings you use in your XML files correspond to an int in R.java, and are hence actually ints. The setId() method will only accept an int value as an argument. You could define your IDs in a constants file, something like:
and then use it as:
You can use tags for that purpose . For example
No you cannot set it to String, the id is int value, even when you set it from XML it is just the resource name of an int value
If you have the references to the views anyway , you can simply save them all into a HashMap, for example using HashMap .
Another alternative , so that you will avoid any typos , is to have an enum as the key of the hashMap , for example : HashMap .