2

I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.

Simply I want to iterate ids by using pre-string values.

Psuedo example:

String pre_value = "textView";

for(int i = 0; i < size; i++) {

    String usable_resource_id = pre_value + Integer.toString(pre_value);
    // So how to use these id like R.id.textView1
    // Cast or something similar

}

Any suggestions?

Ogulcan Orhan
  • 5,170
  • 6
  • 33
  • 49
  • You have to use reflection: [look here][1]. [1]: http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string – drakyoko Mar 15 '12 at 19:32
  • This what you are willing to do is called Metaprogramming (if you ever want to know how to search for it) – Iulius Curt Mar 15 '12 at 19:33

1 Answers1

6

You can use Resources#getIdentifier() to get the identifier from a string.

But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
MByD
  • 135,866
  • 28
  • 264
  • 277