I'm developing an Android application and I just ran into something. I have some anonymous classes (event listeners). They are parameterized from the database. What I did is this:
buttonA.setOnTouchListener(new View.OnTouchListener() {
private Boolean isActive = null;
private boolean isTrigger;
private int onLevel;
private int offLevel;
private int chIdx;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isActive == null) {
Cursor btnSettings = dbHelper.getButtonsTable().fetchButton(1, profileId, currentMode);
...
...
}
return true;
}
Is it considered a good practice to use the Boolean object as a trilean switch (it has a null value if the listener haven't been parameterized yet) or I should use two boolean variables...or maybe an integer?
Do you have and ideas?
Best yet, use an type (probably an enum) with accurate descriptions of the three states. Booleans don't give a lot of information to the person who is calling the function (especially when used as a tristate).
public enum ActiveStatus {
On,
Off,
Unknown
}
I would say either use Boolean with true, false and null or use an enum. I tend to use a Boolean being null as a kind of "don't know yet". If you are using null as something more meaningful than "don't know" you are probably semantically better off going with an enum.
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