I am new to Android programming.
I would like to use a for loop to do the similar things.
Here is my code:
int stubs[] = {R.id.stub_1, R.id.stub_2, R.id.stub_3};
View viewStubs[] = {stub_1, stub_2e, stub_3};
Button buttons[] = {button2, button3, button4};
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
for(int i=0; i<buttons.length; i++){
buttons[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(viewStubs[i] == null){
viewStubs[i] = ((ViewStub)findViewById(stubs[i])).inflate();
}
}
}
}
However, there is an error in onClick method "i":
Cannot refer to a non-final variable i inside an inner class defined in a different method