I know this might sound very basic but here's my problem:
I have an onclickevent listener that's supposed to increment the counter indefinitely when it's clicked:
final int counter = 0;
myimageView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
counter ++;
}
});
The problem is that I can't seem to call the counter from inside the onclick event unless it's set to final. However since it's final I can no longer change its value either.
I tried placing the counter within the onclick event, i.e.:
myimageView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int counter = 0;
counter ++;
}
});
However, clicking it also resets the counter back to zero.
How do I solve this? I want to continuously increment the counter each time I click it, but I can't define the counter outside of the onclick unless it's final, which means I can no longer increment it. And I can't define the counter inside the onclick either since it will only reset its value each time I click it.
Solved:
myimageView2.setOnClickListener(new OnClickListener() {
int counter = 0;
public void onClick(View v) {
counter ++;
}
});
You could declare 'counter' outside the Activity's oncreate method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With