8BitSensei
8BitSensei

Reputation: 301

Java if statement is being ignored

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++;
  }
}});

Upvotes: 0

Views: 2017

Answers (6)

hogni89
hogni89

Reputation: 1754

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.

Upvotes: 0

absentmindeduk
absentmindeduk

Reputation: 136

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++;
    }
});

Upvotes: 0

Jochem
Jochem

Reputation: 2994

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++;
        }
    }
});

Upvotes: 2

Yoztastic
Yoztastic

Reputation: 862

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

Upvotes: 0

djechlin
djechlin

Reputation: 60748

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.

Upvotes: 3

user370305
user370305

Reputation: 109237

From your if statement

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

only

if (index == 0) 

is enough

same as Second one..

if (index == 1) 

Upvotes: 3

Related Questions