0

I want to iterate through a bunch of buttons which are in XML, so I gave each of them an id consisting of a letter and unique number (like q1, q2, q3, etc.).

Now in MainActivity I want to write a for loop that accesses each button by incrementing the id's number:

for (int i = 0; i <10; i++){
String buttonId = "q" + i;
findViewById(R.id.buttonId);
}

Java obviously doesn't allow this, so I'm hoping there are some neat alternatives to doing this.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • ^ From the duplicate I've linked to above, in short, no – Michael Dodd Jun 10 '18 at 20:55
  • I know nothing about Android or, what `findViewById()` does, but often, when somebody asks about generating the name of a variable at run-time, the solution is to use a [`Map`](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) – Solomon Slow Jun 11 '18 at 00:14

0 Answers0