2

What is the difference between true and false in this code?

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            int cbcheck = 1;
        } else {
            int cbcheck = 0;
        }
    }

    @Override
    public void onClick(View v) {
        ;
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.registerbut:
            onCheckedChanged(cbagree, false);
            if (cbcheck == 1) {
                Intent intent = new Intent(this,
                    RegisterScreen2.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            } else if (cbcheck == 0){
            }
            break;
        }
    }

The part where it says onCheckedChanged(cbagree, true); what would happen if I passed false instead?

I wish to make it so if the box is checked the intent will run, otherwise it'll state for you to check the box. I have tried saving the checkboxing and then loading it using savepreferences and loadpreferences but I find that is too much hassle. Is there any other way to accomplish this?

This is in android btw.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Drake
  • 95
  • 1
  • 6
  • 7
    From what I can tell that code doesn't do anything since it declares scope variables and never uses them. – Jesus Ramos Aug 20 '11 at 23:35
  • This is sadly not the first time I've seen this sort of garbage code. It's a practice in a lot of software houses to write pointless code to confuse people into making sure they are rehired. This could be one of those cases. Did you outsource the code before? Or was the code originally written by an other author? – Ali Aug 20 '11 at 23:40
  • This code was written originally by me? Thanks for critiquing my coding as I have only begun programming a week before. However, I do not appreciate that you only gave criticism and not any help to alleviate the problem I have with your extensive knowledge of "coding". – Drake Aug 20 '11 at 23:44
  • @Drake I don't know what kind of 'help' you were 'expecting' but pointless 'code' is still 'pointless', and so are pointless "'" marks. Your code does nothing useful with the 'isChecked' boolean so what its value is is immaterial. – user207421 Aug 20 '11 at 23:59

4 Answers4

4

As it's written, there's no difference since your function onCheckedChanged() literally does nothing: In two local blocks local variables get declared which immediately go out of scope, and there is no net effect of this function at all.

Perhaps you mean to modify some private class member instead?


Suggestion: here's an idea on passing the state change through a private member:

class Thing
{
  private int cbcheck;

  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
  {
    cbcheck = isChecked ? 1 : 0;
  }

  /* ... */
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I want the intent to run if the checkbox is checked. I showed my current code in my edit but the int variable is not being given the values i assigned it in the public void. I was asking how I can get the public void to run in the code. – Drake Aug 20 '11 at 23:40
  • Right, I get it. You have to pass a reference parameter to `onCheckedChange` and modify that one. – Kerrek SB Aug 20 '11 at 23:41
  • modify which one? the public void onCheckedChange or the reference parameter? – Drake Aug 20 '11 at 23:42
  • You have to add a new parameter to the function (but I don't know how -- see [this question](http://stackoverflow.com/questions/3326112/java-best-way-to-pass-int-by-reference), apparently passing an `int` by reference in Java is difficult) and change that new parameter. But even better would be to add a private `int` member to your class and modify that! – Kerrek SB Aug 20 '11 at 23:44
  • This is going in the wrong direction - `onCheckedChanged` is part of the interface `CompoundButton.OnCheckedChangeListener`. – Che Jami Aug 21 '11 at 00:12
  • @Che: Yes, that makes far more sense! :-) – Kerrek SB Aug 21 '11 at 00:20
1

This code looks buggy, onclick checks an instance (or class) variable cbcheck,

if (cbcheck == 1) {

after calling onCheckedChanged but the cbcheck set in onCheckedChanged is a local variable so the change has no effect.

Perhaps onCheckedChanged should be

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        cbcheck = 1;
    } else {
        cbcheck = 0;
    }
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

A much simple way to do this would be to hold a reference to the CompoundButton you are using and call CompoundButton.isChecked in your onClick method.

You shouldn't be calling onCheckChanged yourself and should be registering a listener using CompoundButton.setOnCheckedChangeListener instead (in this case probably cbagree.setOnCheckedChangeListener(this)). Now when the user 'checks' the button, isChecked is true and when the user 'unchecks' the button, isChecked is false.

However, in this case you don't even need to register a listener:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.registerbut:
        if (cbagree.isChecked()) {
            Intent intent = new Intent(this,
                RegisterScreen2.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
        }
        break;
    }
}
Che Jami
  • 5,151
  • 2
  • 21
  • 18
  • would you mind posting an example of your method? Sorry, I have only started programming a week ago and I am just practicing new methods of doing things. I have read 2 books so far but none of them helped me with this problem – Drake Aug 21 '11 at 00:27
  • If you want to take this seriously, I suggest focusing on basic Java before attempting to use Android. A lot of things will bite you in the back later if you don't get your basics right. – Che Jami Aug 21 '11 at 10:20
0

Not my field though true in this case means "boolean isChecked".

frontsideup
  • 2,833
  • 1
  • 21
  • 23