I will send v开发者_如何转开发alue of loop index (variable i) to another class. I decided to use putExtra but to do this variable i have to be a "final".As you know that's impossible because this variable change value for each of the loop.
Here my code:
for (int i = 0; i<=20; i++) {
btn[i].getBackground().setColorFilter(0xFF00FF00,PorterDuff.Mode.MULTIPLY);
btn[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(newgame.this, game.class);
intent.putExtra(game.KEY_MISSION, i);
startActivity(intent);
}
});
}
If you have any ideas or solutions I would be very grateful for your reply. (Sorry for my english)
You can do this
for (int i = 0; i<=20; i++) {
final int j = i;
btn[i].getBackground().setColorFilter(0xFF00FF00,PorterDuff.Mode.MULTIPLY);
btn[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(newgame.this, game.class);
intent.putExtra(game.KEY_MISSION, j);
startActivity(intent);
}
});
}
Create an inner class that takes 'i' as a parameter to the constructor. This is an example of when NOT to use an anonymous inner class, since you are creating multiple instances with different information.
精彩评论