jeudi 31 décembre 2015

If statement depending on the random image shown

I have a imageview that randomly generate 1 out of 2 possibles images clicking on one button.

I want that when one image is showed (R.drawable.aa) and I press other button, a toast is shown.

My problem is that once a random image is shown and click on the other button, nothing happens.

public class buho extends Activity {

// UI components
private Button drawButton;
private Button boton2;
private ImageView cardImage;

// Random object
private final static Random random = new Random();

// The card deck
private final static int[] cardDeck = new int[] {   R.drawable.aa,
        R.drawable.a2,
         };



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buho);

    drawButton  = (Button)findViewById(R.id.drawButton);
    boton2  = (Button)findViewById(R.id.button2);
    cardImage = (ImageView)findViewById(R.id.cardImage);


    drawButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0)
        {cardImage.setImageResource(cardDeck[random.nextInt(cardDeck.length)]);
            boton2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if (cardImage.equals(R.drawable.aa)) {
                        Toast toast = Toast.makeText(buho.this, "si", Toast.LENGTH_LONG);
                        toast.show();
                    } else {

                    }


                }
            });




Aucun commentaire:

Enregistrer un commentaire