onclicklistener - If then else not working in Android/Confusion with final -
i have problem in android. set drawable
conditionally , change boolean
@ same time.
if button
clicked, gets frame , boolean
set true
. if clicked again, frame vanishes , boolean
set false
.
only first part of if working. if click button
, frame set, in case click again, not vanish. 2 drawables
defined correctly.
this sets onclicklisteners
buttons
within array
.
(int j = 0; j < dice.length; j++) { final button temp = dice[j]; final boolean locked = islocked[j]; final int finalj = j; dice[j].setonclicklistener(new view.onclicklistener() { public void onclick(view v) { setlock(locked, finalj, temp); } }); } }
this called method islocked
array
contains booleans
belonging buttons
. know not smartest/best solution, due rest of code required.
public void setlock(boolean locked, int finalj, button button) { if (!locked) { button.setbackgrounddrawable(getresources().getdrawable(r.drawable.dice_locked)); } else { button.setbackgrounddrawable(getresources().getdrawable(r.drawable.dice)); } islocked[finalj] = !islocked[finalj]; }
could help?
final boolean locked = islocked[j];
here value frozen , never changes in following calls inside onclicklistener.onclick(view v)
.
the array item changed though (the 1st time - have observed).
to make right, rid of locked
param:
public void setlock(int finalj, button button) { boolean locked = islocked[finalj]; .... islocked[finalj] = !islocked[finalj]; }
Comments
Post a Comment