Epcilon
Epcilon

Reputation: 95

Declaring button variables as an array with a for loop android

I managed to create buttons in a for loop and saw no reason why not to declare my varibles inside it too. Unfortunately eclipse only identifies the "bt" and doesn't want to replace my [i] with the number it represents in the loop and as a result find the correct id in my layout. Any thoughts on how to make this work? I'm also greatful for any other solution as beatiful as mine, which doesn't work ;)

Button [] bt = new Button[6];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_layout);

    bt[0] = (Button) findViewById(R.id.bt0);
    bt[1] = (Button) findViewById(R.id.bt1);//This is what i'm trying to replace
    bt[2] = (Button) findViewById(R.id.bt2);
    bt[3] = (Button) findViewById(R.id.bt3);
    bt[4] = (Button) findViewById(R.id.bt4);
    bt[5] = (Button) findViewById(R.id.bt5);


    for (int i=0; i<6; i++) {
        final int b = i;
        bt [i] = (Button)findViewById(R.id.bt[i]);    <----//Whith this
        bt [i].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent myIntent = new Intent(Start.this, MainActivity.class);
                myIntent.putExtra("players", b);
                startActivity(myIntent);

                //startActivity(new Intent(Start.this, MainActivity.class));
            }
        });

    }
}

Upvotes: 6

Views: 10106

Answers (2)

Lawrence Choy
Lawrence Choy

Reputation: 6108

I would do the following:

private static final int[] idArray = {R.id.bt0, R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5};

private Button[] bt = new Button[idArray.length];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_layout);

    for (int i=0; i<idArray.length; i++) {
        final int b = i;
        bt [b] = (Button)findViewById(idArray[b]); // Fetch the view id from array
        bt [b].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent myIntent = new Intent(Start.this, MainActivity.class);
                myIntent.putExtra("players", b);
                startActivity(myIntent);

                //startActivity(new Intent(Start.this, MainActivity.class));
            }
        });

    }
}

If you want to add or remove buttons, just add it to idArray and all other things are dynamic already.

Upvotes: 12

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

I think if you have group of similar buttons - they all placed inside 1 parent on layout (LinearLayout or RelativeLayout or something else). You can take get parent and retrieve all children. This way you don't need to specify id for each button.

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons);
List<Button> buttons = new ArrayList<Button>();
for (int i = 0; i < buttonsView.getChildCount(); i++) {
  buttons.add((Button) buttonsView.getChildAt(i));
}

Also you can store button's number in it's tag so you don't need to create final int variables:

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons);
List<Button> buttons = new ArrayList<Button>();
View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(Start.this, MainActivity.class);
        myIntent.putExtra("players", (Integer) v.getTag());
        startActivity(myIntent);
        //startActivity(new Intent(Start.this, MainActivity.class));
    }
};
for (int i = 0; i < buttonsView.getChildCount(); i++) {
  Button button = (Button) buttonsView.getChildAt(i);
  button.setTag(i);
  button.setOnClickListener(listener);
  buttons.add(buttons);
}

Upvotes: 0

Related Questions