I've learned how to create views dynamically, but is it possible to NAME them dynamically? For example, how can I create, say, 3 TextViews named "myTV1", "myTV2" and "myTV3" using a for loop?
Asked
Active
Viewed 18 times
0
-
not much of an android dev, but if it isn't api supported, it's probably reflection supported in some way – Rogue Nov 06 '16 at 02:33
-
No, it's not reflection supported -- you cannot create dynamically created names for variables in Java, and more importantly, nor do you want to. – Hovercraft Full Of Eels Nov 06 '16 at 02:36
-
Variable "names" aren't really all that important, don't exist for many objects, and *almost* don't exist in compiled code. And if an object is referred to by several variables, which one represents the "name" for this object? Much more important are *object **references*** and how they can be obtained. – Hovercraft Full Of Eels Nov 06 '16 at 02:38
-
If you must associate an object with a String, a clean way to do this is by using a Map such as a `HashMap
` or `HashMap – Hovercraft Full Of Eels Nov 06 '16 at 02:38` depending on which you desire to use as the key, but again don't put too much reliance on variable names since non-final variables can change references at the drop of a hat, and objects can be referred to by more than one variable. -
Or if you want to associate your variable with monotonically increasing numbers, then use an ArrayList. – Hovercraft Full Of Eels Nov 06 '16 at 02:39
-
You can Set TAG value for dynamic views. – Divyesh Patel Nov 06 '16 at 05:18