0

Background

I am creating buttons dynamically in a for loop by following Pragnesh Ghota's solution of one onClick listener for every button in the format of dymmeh's individual initialization solution:

LinearLayout someLayout = (LinearLayout) findViewById(R.id.theRoom);
    for (int i = 0; i < neededButtons.length; i++){
        neededButtons[i] = new Button(this);
        neededButtons[i].setText(names[i]);
        neededButtons[i].setOnClickListener(this);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
    }

In addition, I am making one onClick listener by implementing View.OnClickListener in the actvity class. My class is defined as such:

public class RecallActivity extends AppCompatActivity implements View.OnClickListener{
    ...
}

I have followed the other steps of Pragnesh Ghota's solution with success. However...

Problem

The fourth step of Pragnesh Ghota's solution mentions the use of a case statement to check if any of the buttons have been clicked. This works when the amount of buttons is known. However, since I am following the format laid out in dymmeh's solution, I do not know how many buttons I am checking until execution time.

Question

How do I do a control flow statement within an overrided onClickMethod for a dynamic amount of buttons?

isakbob
  • 1,439
  • 2
  • 17
  • 39

3 Answers3

1

Just create a new OnClickListener for each button when you're creating them.

LinearLayout someLayout = (LinearLayout) findViewById(R.id.theRoom);
for (int i = 0; i < neededButtons.length; i++){
    neededButtons[i] = new Button(this);
    neededButtons[i].setText(names[i]);
    neededButtons[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // add your click listener code here
                    }
                })
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
}
terencey
  • 3,282
  • 4
  • 32
  • 40
0

you can set a id for button .just like this:

LinearLayout someLayout = (LinearLayout) findViewById(R.id.theRoom);
for (int i = 0; i < neededButtons.length; i++){
    neededButtons[i] = new Button(this);
    neededButtons[i].setText(names[i]);
    neededButtons[i].setId(i);
    neededButtons[i].setOnClickListener(this);
    ...
    ); 
}

then find view by id in OnClickListener. for example:

public class RecallActivity extends AppCompatActivity implements View.OnClickListener{
  @overide
  public void onClick(View view){
     if(view.getId == 0){
        .....
   }
 }
}
abby
  • 157
  • 4
0

The simplest solution is using setTag and getTag for your buttons. You can use an object with setTag and getTag. Whenever you're creating a button, set the tag for it:

for (int i = 0; i < neededButtons.length; i++){
    neededButtons[i] = new Button(this);
    neededButtons[i].setText(names[i]);
    neededButtons[i].setTag(names[i]);
    // or you can use the index as the tag with:
    // neededButtons[i].setTag(i);
    neededButtons[i].setOnClickListener(this);
}

Then you do something for each button by checking the tag:

@Override
public void onClick(View v) {
    doSomething(v.getTag());
}

private void doSomething(Object tag) {
  // in case your tag is the index, than you can convert it to 
  // integer and use switch case
  int index = (int) tag;
  switch(index) {
    case 1:
      ...
      break;
    case 2:
      ...
      break;
    ...
  }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96