In the following code, the first if
statement complains that current_guy
can't be a final
but the second if
statement complains that current_guy
isn't a final. I am out of ideas.
final int current_guy=0;
if (a.equals("bf")){
current_guy=1;
}
guy1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (current_guy==1) {
// TODO Auto-generated method stub
}
}
The reason the first one is complaining is because you can't set a final variable. The reason it's complaining that it's not final is that you're creating what is called an anonymous inner class. So your OnClickListener is a completely separate class and can't access variables from your main class at runtime. This is why it has to be Final. Then when the compiler is building your app it knows what that value is and can substitute it in there. If you want to have this variable be, well variable, then you have to pass the value into the OnClickListener so that it can get the updates as the variable changes.