NewbieontheSide
NewbieontheSide

Reputation: 37

if else statement in android

I was new in Android and found some good tutorials on the internet, so I tried a simple activity with an if-else statement. I'm trying "correct and wrong" prompt/Toast:

Button page1 = (Button) findViewById(R.id.button2);
       page1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
                if (iv1.equals(R.drawable.airplane1)) {
                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane2)) {
                    Toast.makeText(getApplicationContext(), "Please put an answer",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane3)){
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

       });

I'm not sure what is wrong in my if-else statement but never prompts at all. I tried removing the (iv1.equals(R.drawable.airplane3)) and (iv1.equals(R.drawable.airplane2)) then it only shows the wrong Toast. I can't get seem to make the correct to prompt me.

Here is the full code of my class:

public class MainActivity extends Activity {
private static final Random imagerandom = new Random();

private static final Integer[] Imagesnumber = 
    { R.drawable.airplane1, R.drawable.airplane2, R.drawable.airplane3, };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Integer a = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)];
    final ImageView iv = (ImageView) findViewById(R.id.imageView1);

    View nextButton = findViewById(R.id.button1);
    nextButton.setOnClickListener(new Button.OnClickListener() {
         public void onClick(View V) {
              int resource = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)];
              iv.setImageResource(resource);
         }
    });
    Button page1 = (Button) findViewById(R.id.button2);
       page1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
                if (iv1.equals(R.drawable.airplane1)) {
                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane2)) {
                    Toast.makeText(getApplicationContext(), "Please put an answer",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane3)){
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

       });




}

}

Upvotes: 3

Views: 18462

Answers (4)

Zabador
Zabador

Reputation: 727

iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane1).getConstantState())                                                                                                                                                                                                                                                                                                                                                                                                                                              

Upvotes: 2

invertigo
invertigo

Reputation: 6438

R.drawable.airplane* is an int, which means you are comparing an ImageView object with an int, which is never true. Use iv.getDrawable().equals(getResources().getDrawable(R.drawable.airplane1)); to compare to Drawable objects.

This is probably not the best option performance wise, you may want to keep track of the index of the displayed image as a class variable, and do a conditional based on that (or something along those lines).

Upvotes: 2

gpasci
gpasci

Reputation: 1440

You are comparing ImageView instances and ints, the right way to check which view was clicked is:

if (iv1.getId() == R.id......) { ...

this way you are comparing an int with another int

Upvotes: 0

Pragnani
Pragnani

Reputation: 20155

You can't compare type of ImageView with the type of int

iv1.equals(R.drawable.airplane1)

this is wrong

try this

iv.getDrawable()==getResources().getDrawable(R.drawable.airplane1)

instead

Upvotes: 1

Related Questions