0
View.OnClickListener handleOnClick(ToggleButton mButton, boolean mBoolean) {
    return new View.OnClickListener() {
        public void onClick(View v) {
            mBoolean = !mBoolean;
            updateUi(mButton, mBoolean);
        }
    };
}

mBoolean and mButton "is accessed from within inner class, needs to be declared final"

But when I do, I can't do the mBoolean =!mBoolean because it says I can't change a final variable.

Liang Cui
  • 33
  • 5

2 Answers2

6

Just pass mBoolean as its opposite updateUi(mButton, !mBoolean);

voice
  • 1,326
  • 1
  • 11
  • 16
  • but it still says I can't do updateUi(mButton, !mBoolean); because those needs to be declared final. Is there anyway I can do this without declaring them final? Because I want to know how to do that since I need to change the variables in the future. – Liang Cui Jan 16 '16 at 09:10
  • I need more of your code. Perhaps you don't need it to be final, and you can pass a boolean to this code with the same value as mboolean. Then, from wherever mboolean is declared, you change it there. – voice Jan 16 '16 at 16:07
  • Thank you for the help! I really appreciate your effort. My code is very different and messy now and it'll be even more confusing to read. Basically the idea is very simple. What I'm trying to do is: Create a ToggleButton array, as well as a boolean array to track their state. When onClick is triggered for any specific button, it's state will change (let's say from off to on), and so will it's drawable (let's say from "button1_off.png" to "button1_on.png". I'm just starting to learn so I can't really think of a smarter way to do this. Thanks again! – Liang Cui Jan 17 '16 at 06:15
  • P.S. And I know making everything global variable is a bad thing to do so I'm trying to avoid it. Even when I try to do so, I still can't even access the "i" variable in the "for" line, from within the inner class, so I can't use a for repeat to loop a specific function, this is driving me crazy! Dx Guess I'll have to make that i final as well as go with 'for (final int i = 0; i < 4; i++)' ? EDIT: Great I just found out I can't do that either because 'i++' is trying to change a variable declared final and that's not allowed yay! – Liang Cui Jan 17 '16 at 06:24
0

If you needs change value of external variable from lisneter - use final array instead of primitive.

waxtah
  • 306
  • 2
  • 10