0

I have created some buttons dynamically in a class with this code: The variable drawView.getNumeroMallas() could be different each time

     for(int i=0;i<drawView.getNumeroMallas();i++){

                Button buttonMalla = new Button(this);
                buttonMalla.setText("Malla "+(i+1));
            buttonMalla.setId(i+1);
                buttonMalla.setTag(Boolean.FALSE);
}

And now I want to find these buttons inside a loop in another class. Any idea?

1 Answers1

0
  1. You can store them in the list and then access this list from another part of the code with the getter method.

  2. you should not use setId() like this, because your ids can, actually, collide with another ones, so you should use View.generateViewId().

Tzoiker
  • 1,354
  • 1
  • 14
  • 23
  • 1
    `View.generateViewId()` is only available in SDK 17 and up do you still have to use that – tyczj Mar 17 '14 at 19:04
  • @tyczj No, and I wouldn't use it in that case. I would recommend using the key generation method listed in this solution by phantomlimb: http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – zgc7009 Mar 17 '14 at 20:41
  • 1
    @zgc7009 sorry that wasnt a question, there was a typo. `do you still have to use that` was suppose to be `so you still have to use that` – tyczj Mar 17 '14 at 20:44