-2

I want to check if the image of an Image Button equals another image. This is what I have been trying.

if(String.valueOf(image1.getId()) == (selectedQ.image))){
                ResultAlertFragment alertDialog = new ResultAlertFragment();
                alertDialog.show(getFragmentManager(), "list alert");
                }
                else if(!(String.valueOf(image1.getId()) == pictureCorrect)){
                    ResultAlertFragmentIncorrect alertDialog = new ResultAlertFragmentIncorrect          ();
                    alertDialog.show(getFragmentManager(), "list alert");
                }

This is where my image selectedQ is set

RandomImage[] questionArray = { imageBoy, imageGirl, imageMan,
            imageWoman, imageCow, imageDog, imageCat, imageFox };

    // random int for Button
    Random randomeQuestion = new Random(System.currentTimeMillis());

    int nextQuestion = randomeQuestion.nextInt((questionArray.length) - 1);
    selectedQ.person = questionArray[nextQuestion].person;
    selectedQ.image = questionArray[nextQuestion].image;
    selectedQ.displayed = questionArray[nextQuestion].displayed;

    // Selecting a random question
    Random randomerSelection = new Random(System.currentTimeMillis());
    int randomposition = randomerSelection.nextInt(3);
    ImageView imagePosition = (ImageView) findViewById(imageViews[randomposition]);
    imagePosition.setImageResource(selectedQ.image);

    ArrayList<Integer> imagesAlreadyInUse = new ArrayList<Integer>();
    for (int i = 0; i < 4; i++) {
        if (!(i == randomposition)) {
            int nextImage = 0;
            Random randomImageForButtons = new Random(
                    System.currentTimeMillis());

            Boolean condition = false;
            while (condition == false) {
                nextImage = randomImageForButtons
                        .nextInt((questionArray.length) - 1);
                if (!(nextImage == nextQuestion)) {
                    if (!(imagesAlreadyInUse
                            .contains(questionArray[nextImage].image))) {
                        imagesAlreadyInUse
                                .add(questionArray[nextImage].image);
                        condition = true;
                    }
                }
            }

            int v = imageViews[i];
            ImageView iv = (ImageView) findViewById(v);
            iv.setImageResource(questionArray[nextImage].image);
            questionArray[nextImage].displayed = true;
        }
    }

    tv1.setText(selectedQ.person);
    pictureCorrect = String.valueOf(selectedQ.image);

My app asks a user a question. This question is a question object that has a picture and a string. The user must select 1 picture out of 4 that best suits the question. These are randomly generated. I am not sure how to check if they are correct i.e. the picture they selected matches the question.

Can anyone help?

1 Answers1

1

You cannot track the imageview's drawable resource.See this but as a workaround you can do something like this using tags

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView0.setImageResource(R.drawable.image0);
imageView0.setTag(R.drawable.image0);

imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView0.setImageResource(R.drawable.image1);
imageView1.setTag(R.drawable.image1);

then you can compare the tags in your code like

if( view1.getTag() == view2.getTag())
{
  //do something
}
Community
  • 1
  • 1
mchouhan_google
  • 1,326
  • 1
  • 17
  • 24