lundi 24 décembre 2018

ImageView will not compare Drawables Android

I am wanting to create an application where 7 Drawables are being compared and the code must check to see whether the user has clicked on the correct and corresponding image that has been displayed in imageView. The imageView generates a randomly picked Drawable from an array. However, with my current code, it is always outputting "Well Done!" in my textView even if the user has selected the incorrect Drawable that has been displayed by imageView. I have created the same android:onClick="clickedEmoji" for all 7 Drawables in activity_main.xml.

public class MainActivity extends AppCompatActivity {

public ImageView imageView;
public Random random;
public TextView textView;
int easy[] = {R.drawable.face_with_tears_of_joy, R.drawable.grinning_face, R.drawable.grinning_face_with_smiling_eyes};
int hard[] = {R.drawable.face_with_tears_of_joy, R.drawable.grinning_face, R.drawable.grinning_face_with_smiling_eyes,
        R.drawable.rolling_on_the_floor_laughing, R.drawable.smiling_face_with_open_mouth, R.drawable.smiling_face_with_open_mouth_and_cold_sweat,
        R.drawable.smiling_face_with_open_mouth_and_smiling_eyes};
public ImageView imageView1;
public ImageView imageView2;
public ImageView imageView3;
public ImageView imageView4;
public ImageView imageView5;
public ImageView imageView6;
public ImageView imageView7;

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

    imageView1 = (ImageView) findViewById(R.id.face_with_tears_of_joy);
    imageView2 = (ImageView) findViewById(R.id.grinning_face);
    imageView3 = (ImageView) findViewById(R.id.grinning_face_with_smiling_eyes);
    imageView4 = (ImageView) findViewById(R.id.rolling_on_the_floor_laughing);
    imageView5 = (ImageView) findViewById(R.id.smiling_face_with_open_mouth);
    imageView6 = (ImageView) findViewById(R.id.smiling_face_with_open_mouth_and_cold_sweat);
    imageView7 = (ImageView) findViewById(R.id.smiling_face_with_open_mouth_and_cold_sweat);
    imageView = (ImageView) findViewById(R.id.imageView);
    random = new Random();
    textView = (TextView) findViewById(R.id.textView);

    int randomInt = random.nextInt(hard.length);
    imageView.setBackgroundResource(hard[randomInt]);

}

public void clickedEmoji(View view) {

    Drawable d = imageView.getDrawable();

    if (d == imageView.getDrawable()) {
        textView.setText("Well Done!");
    } else {
        textView.setText("Unlucky!");
    }
}

Aucun commentaire:

Enregistrer un commentaire