0

I'm making a simple app where the user clicks a button which changes a TextView to the corresponding string, but when my first if statement is fulfilled it does not go on to fulfill the following if statement which should be.

if (index == 0 && index > -1 && index < 5) {
  one.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      index++;
      text1.setText("1");
    }
  });

This is my first if statement, it sets the TextView to "1", and then should add to the integer "index", this turns the index's value to "1", which should end this statement because it no longer qualify, which will begin the following if statement.

if (index == 1 && index > -1 && index < 5) {
  one.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      text2.setText("1");
      index++;
    }
  });

Now because of the previous if statement setting the index's variable to "1" this if statement should begin and the previous end, but this is not the case, even though the variables no longer qualify, it doesn't stop and the next if statement does not begin. It's as though the if statement is being ignored.

UPDATE.

I fixed my problem and here's what I changed the code to:

one.setOnClickListener(new View.OnClickListener(){public void onClick(View v){
  if(index == 0){
    text1.setText("1");
    index++;
  }else if(index == 1){
    text2.setText("1");
    index++;
  }else if(index == 2){
    text3.setText("1");
    index++;
  }else if(index == 3){
    text4.setText("1");
    index++;
  }
}});
3
  • 6
    if (index == 0 && index > -1 && index < 5) is equivalent to if (index == 0) Aug 28, 2012 at 14:36
  • 4
    problems with basic code flow ? ... your if statment is checked only once (prolly in onCreate) not at every button click ...
    – Selvin
    Aug 28, 2012 at 14:37
  • 1
    if (index == 0 && index > -1 && index < 5) makes very little sense. Aug 28, 2012 at 14:37

6 Answers 6

3

From your if statement

if (index == 0 && index > -1 && index < 5)

only

if (index == 0) 

is enough

same as Second one..

if (index == 1) 
3
  • 1
    Also put if condition in button's onClick() to verify condition on every button's click..
    – user370305
    Aug 28, 2012 at 14:40
  • This answer does not really solve his problem I think. The comment of user370305 is important though.
    – Jochem
    Aug 28, 2012 at 14:42
  • I do realize that they were kind of pointless, but i was advised to by the person giving me the task
    – 8BitSensei
    Aug 28, 2012 at 15:14
3

That's not how if statements work. When the condition becomes false control flow does not suddenly leave the block and jump to some other if-block with a true condition.

2
  • +1 because I think you understood the question (which wasn't easy)
    – erikkallen
    Aug 28, 2012 at 15:14
  • Jochem has this right, though - s/he also answered the right question but did so with more completeness.
    – djechlin
    Aug 28, 2012 at 15:30
2

Your problem is that your index++ statement is inside the onClick callback. So this code is not executed until the button gets clicked. By that time, your second if statement will long be executed. In other words:

Your first if statement is true, this adds an onClick listener to the button. Your second statement is false, this does nothing. The user clicks on the button. Now only the code inside that first callback is executed: index is increased and the text is set to "1". That is all.

[EDIT this is probably what you want]

one.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if ( index == 0 ) {
            index++;
            text1.setText("1");
        } else if (index == 1 ) {
            text2.setText("1");
            index++;
        }
    }
});
1
  • but my second if statement isn't executed until "index's" variable = 1, this isn't true until the button is pressed?
    – 8BitSensei
    Aug 28, 2012 at 15:36
0

After index == 1 or index == 0 what is the relevance of the other conditions?

0

Another viable solution:

if ((index > -1) && (index < 5)) {
    one.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            text2.setText(index.toString());
            index++;
    }
});

-Obviously this depends on the logic you require specifically, if you do require another occurrence when index == 0, you would have to alter this format:

if (index == 0) {
    //Whatever you want to do here... eg:
    one.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            text2.setText("0");
            index++;
        }
    });
}
else if ((index > -1) && (index < 5)) {
    one.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            text2.setText(index.toString());
            index++;
    }
});
1
  • In this scenario you have a if statement surrounding your onClickListener. isn't it better to put the if statement inside your onClickListener?
    – hogni89
    Aug 28, 2012 at 15:17
0

The reason that it doesn't work, is that the listener probably is added in "onLoad" method or in the constructor of the class.

Since your setOnClickListener is surrounded with a if statement, only the listener is only set if the statement is true. In your case the first statment is true, while the seccond statement is false.

This means that only the first listner is added, and everytime the button is clicked, the text1.setText("1") is called.

You should put your if statement inside your onClickListener, and not around the logic that sets the listener. So the following code:

  if (index == 0 && index > -1 && index < 5) {
        one.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                index++;
                text1.setText("1");

            }
        });

should be

one.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    if (index == 0) {
      index++;
      // Do stuff
    } else if(index == 1) {
      index++;
      // Do stuff
    }
  });
}

Every time the user hits the button 'one', the onClick is called. Inside the onClick the statement is calculated, and the correct "Do stuff" is executed.

2
  • This will not compile. The if statement is immediately after opening the anonymous class definition.
    – Jochem
    Aug 28, 2012 at 14:47
  • At least keep the original indentation
    – Steve Kuo
    Aug 28, 2012 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.