-2

I have several layout elements which I want to process as an array, for example:

  for (int j=0; j< N; j++)
        ((TextView)findViewById(R.id.groupStart+j)).setText(getRowText(j));

The problem is that Android doesn't understand ID definition like

   android:id="@+id/groupStart+1"

Perhaps I can't find right syntax. I can assure certain IDs for element if I add them in a layout grammatically, but I want to define them in resource. I can't also edit R.java because it is automatically generated. Any recommendations?

Don Cesar D'Bazar
  • 321
  • 1
  • 2
  • 9

1 Answers1

1

Use this :

for(int i=0; i<5; i++){
    int resID = getResources().getIdentifier("groupStart"+i, "id", getPackageName());
    view = findViewById(resID);
}

Where ids are:

android:id="@+id/groupStart1"
android:id="@+id/groupStart2" 
.
.
.
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103