0

I am adding OnClickListener in loop for buttons, the problem is it the i variable needs to be final, but then a can`t increase him.

My code:

ArrayList<Button> buttonList  = new ArrayList<Button>();
buttonList.add((Button)findViewById(R.id.mov1_btn));
buttonList.add((Button)findViewById(R.id.mov2_btn));
buttonList.add((Button)findViewById(R.id.mov3_btn));
buttonList.add((Button)findViewById(R.id.mov4_btn));
buttonList.add((Button)findViewById(R.id.mov5_btn));
buttonList.add((Button)findViewById(R.id.mov6_btn));

for(int i = 0;i<buttonList.size();i++) 
    {
        buttonList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    buttonList.get(movCnt).setBackgroundResource(R.drawable.mov1n);
                    buttonList.get(i).setBackgroundResource(R.drawable.mov1o);  
                    movCnt = i;

                }
            });
    }
0

4 Answers 4

5

Since i can't be final, you have to declare another variable.

for (int i = 0; i < buttonList.size(); i++) {
    final int current = i;  

    buttonList.get(i).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonList.get(movCnt).setBackgroundResource(R.drawable.mov1n);
            buttonList.get(current).setBackgroundResource(R.drawable.mov1o);
            movCnt = current;
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Don't set the variable i to final, instead just set a tag to the view. This way the value can always stay with the view and can be used whenever needed.

for(int i = 0;i<buttonList.size();i++) 
{
    buttonList.setTag(i);
    buttonList.get(i).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                buttonList.get(movCnt).setBackgroundResource(R.drawable.mov1n);
                buttonList.get(i).setBackgroundResource(R.drawable.mov1o);  
                movCnt = (Integer)v.getTag();

            }
        });
}

Comments

1
//...
for(int i = 0;i<buttonList.size();i++) 
{
    final int current = i;

    buttonList.get(ii).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                buttonList.get(movCnt).setBackgroundResource(R.drawable.mov1n);
                buttonList.get(current).setBackgroundResource(R.drawable.mov1o);  
                movCnt = current;

            }
        });
}

Comments

1

You need to make the button final not the i, Here is an example for it should be done:

ArrayList<Button> list = new ArrayList<Button>();

        list.add((Button) findViewById(R.id.BTN_login));
        list.add((Button) findViewById(R.id.BTN_login));
        list.add((Button) findViewById(R.id.BTN_login));
        list.add((Button) findViewById(R.id.BTN_login));
        for (int i = 0; i < list.size(); i++)
        {
            final Button tempButton = list.get(i);
            tempButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                     tempButton.setBackgroundResource(R.drawable.mov1n);
                     tempButton.setBackgroundResource(R.drawable.mov1o);  


                }
            });

        }

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.