Please help me with this, i never ever understanded how things work inside onClickListener, so:
Got my own class:
public class ItemFrame extends LinearLayout{
int item_id;
public ItemFrame(Context context){
super(context);
}
public int getItem_id() {
return item_id;
}
public void setItem_id(int item_id) {
this.item_id = item_id;
}
}
I initalize one and add an onClickListener:
ItemFrame myItemFrame = new ItemFrame(this);
myItemFrame.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//ERROR HERE:
//Cannot refer to a non-final variable myItemFrame inside an inner class
//defined in a different method
//Quick fix: Change modifier of 'myItemFrame' to final.
myItemFrame.setItemId(100);
}
});
So… My question is:
How can i set a data tag of my class, inside the onClickListener???
If i change it to final as Eclipse says, i cant modify it since it is final.
Does it make any sense ?
The OnClickListener that you created is an anonymous inner class and can only refer to final values.
Simply change:
to be:
You can still modify the ItemFrame object that you created. You just can’t reassign a new one to the myItemFrame variable.