I have a few commands that I only want to run if a question is being answered correctly.
What I did was take 4 buttons, with each one changing the value of variable isCorrect.
If the button pressed is the correct answer, isCorrect will be 2. If not, it will be 1.
Then I am running an if statement to check if the question was answered correctly, and if that is the case the commands under if will run.
The problem is that the if runs runs before the isCorrect value is changed.
That means that even before you press an answer the if statement already runs which doesn't allow me to do what I am trying to do. I wanted to know how can I make sure that the if statement isn't being ran until the button is pressed. (Worth noting that I don't want to put the if statement inside of the button onClick)
triangles[pieces[2].getTriangleOn() + i2].getTriangleIV().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (triangles[pieces[2].getTriangleOn() + i2].getIsSpecial() == 0) {
isCorrect = 2;
} else if (triangles[pieces[2].getTriangleOn() + i2].getIsSpecial() == 1) { //4 שאלות
Random r = new Random();
int i = r.nextInt(90);
for (int qnum = 0; qnum < 90; qnum++) {
if (questions4[qnum].getQuestionNum() == i) {
Answer1.setText(questions4[qnum].getAnswer1());
Answer2.setText(questions4[qnum].getAnswer2());
Answer3.setText(questions4[qnum].getAnswer3());
Answer4.setText(questions4[qnum].getAnswer4());
questionview.setText(questions4[qnum].getQuestion());
questionbackground.setVisibility(View.VISIBLE);
Answer1.setVisibility(View.VISIBLE);
Answer2.setVisibility(View.VISIBLE);
Answer3.setVisibility(View.VISIBLE);
Answer4.setVisibility(View.VISIBLE);
questionview.setVisibility(View.VISIBLE);
CorrectAnswer = questions4[qnum].getCorrectAnswer();
}
}
Answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (CorrectAnswer == 0) {
isCorrect = 2;
Toast.makeText(MainGScreen.this, "Correct", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
} else {
isCorrect = 1;
Toast.makeText(MainGScreen.this, "Incorrect", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
}
questionbackground.setVisibility(View.GONE);
Answer1.setVisibility(View.GONE);
Answer2.setVisibility(View.GONE);
Answer3.setVisibility(View.GONE);
Answer4.setVisibility(View.GONE);
questionview.setVisibility(View.GONE);
}
});
Answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (CorrectAnswer == 1) {
isCorrect = 2;
Toast.makeText(MainGScreen.this, "Correct", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
} else {
isCorrect = 1;
Toast.makeText(MainGScreen.this, "Incorrect", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
}
questionbackground.setVisibility(View.GONE);
Answer1.setVisibility(View.GONE);
Answer2.setVisibility(View.GONE);
Answer3.setVisibility(View.GONE);
Answer4.setVisibility(View.GONE);
questionview.setVisibility(View.GONE);
}
});
Answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (CorrectAnswer == 2) {
isCorrect = 2;
Toast.makeText(MainGScreen.this, "Correct", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
} else {
isCorrect = 1;
Toast.makeText(MainGScreen.this, "Incorrect", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
}
questionbackground.setVisibility(View.GONE);
Answer1.setVisibility(View.GONE);
Answer2.setVisibility(View.GONE);
Answer3.setVisibility(View.GONE);
Answer4.setVisibility(View.GONE);
questionview.setVisibility(View.GONE);
}
});
Answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (CorrectAnswer == 3) {
isCorrect = 2;
Toast.makeText(MainGScreen.this, "Correct", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
} else {
isCorrect = 1;
Toast.makeText(MainGScreen.this, "Incorrect", Toast.LENGTH_SHORT).show();
Toast.makeText(MainGScreen.this, "IsCorrect" + isCorrect, Toast.LENGTH_SHORT).show();
}
questionbackground.setVisibility(View.GONE);
Answer1.setVisibility(View.GONE);
Answer2.setVisibility(View.GONE);
Answer3.setVisibility(View.GONE);
Answer4.setVisibility(View.GONE);
questionview.setVisibility(View.GONE);
}
});
if(isCorrect == 2)
{
//Commmands here
}