user2490332
user2490332

Reputation: 13

value of final int to int

I am currently fooling around with the android SDK (eclipse) and i am trying to make a simple button-based TicTacToe game.

I made an array where i save the Buttons and i am trying to make the ClickEvents dynamically:

Here is the Code (Which works so far):

public class MainActivity extends Activity 
{
final Button[] buttons = new Button[9];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttons[0] = (Button) findViewById(R.id.button1);
    buttons[1] = (Button) findViewById(R.id.button2);
    buttons[2] = (Button) findViewById(R.id.button3);
    buttons[3] = (Button) findViewById(R.id.button4);
    buttons[4] = (Button) findViewById(R.id.button5);
    buttons[5] = (Button) findViewById(R.id.button6);
    buttons[6] = (Button) findViewById(R.id.button7);
    buttons[7] = (Button) findViewById(R.id.button8);
    buttons[8] = (Button) findViewById(R.id.button9);

    for(int i = 0;i < 9;i++)
    {
        buttons[i].setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                // 
            }
        });
    }
}

}

The problem comes when i try to reference to a button in the onClick-Method using 'i'. For example:

            @Override
            public void onClick(View arg0)
            {
                             //buttons[0].setText("X"); works by the way
                buttons[i].setText("X");

            }

Eclipse tells me: "Change modifier of 'i' to final"

Well but if 'i' would be final, i wouldn't be able to increase 'i' in the loop.

So i tried this:

            @Override
            public void onClick(View arg0)
            {
                            final int position = i;
                buttons[position].setText("X");
            }

Which ends up resulting with the same Error "Change modifier of 'i' to final"

So my Question is: How can i make this work?

I have tried google but i wasn't able to find anything and this really is the first time i ever encountered this problem .

Hope you guys can help me out!


EDIT:

Well the solution was simple, here is the now working code:

        for(int i = 0;i < 9;i++)
    {
        final int position = i;
        buttons[i].setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                buttons[position].setText("X");
            }
        });
    }

Upvotes: 1

Views: 145

Answers (3)

Niranjan
Niranjan

Reputation: 1844

Have a separate class which implements View.OnClickListener and have a constructor to accept the index value

Upvotes: 0

Cata
Cata

Reputation: 11211

You can do something like this:

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

      final int position = i;

      buttons[i].setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View arg0)
         {
            buttons[position].setText("X"); 
         }
      });
}

Upvotes: 2

Lingviston
Lingviston

Reputation: 5671

Create another final int var in your loop set it the same value as i and reference this var from you listeners.

for(int i = 0;i < 9;i++)
{
    final int index = i;
    buttons[i].setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            buttons[index].setText("X");
        }
    });
}

Upvotes: 1

Related Questions