Search code examples
javaandroidimageview

How can I compare images


I am developing a multiple choice quiz app in which I am using images as questions and answers the problem here is that I don't know how to compare images from array.

In onclick method I am comparing the clicked choice with the correct answer stored in array but it's not working; I even used setTag and getTag methods for choices. I don't know what is wrong with the code, I have tried everything.

Here is my code:

public class counting extends AppCompatActivity {
    ImageView choice_one, choice_two, choice_three, choice_four;
    ImageView question;
    MediaPlayer mp;
    private Questions mQuestions = new Questions();
    private int mAnswer;
    private int mQuestionsLength = mQuestions.mQuestions.length;

    Random r;

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

        r = new Random();

        choice_one=(ImageView)findViewById(R.id.choice_1);
        choice_two=(ImageView)findViewById(R.id.choice_2);
        choice_three=(ImageView)findViewById(R.id.choice_3);
        choice_four=(ImageView)findViewById(R.id.choice_4);

        choice_one.setTag(1);
        choice_two.setTag(2);
        choice_three.setTag(3);
        choice_four.setTag(4);

        question=(ImageView)findViewById(R.id.question);

        updateQuestions(r.nextInt(mQuestionsLength));

        choice_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_one) == mAnswer ){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });

        choice_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_two) == mAnswer){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });

        choice_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_three) == mAnswer){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });

        choice_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageResource(choice_four) == mAnswer){
                    mp=MediaPlayer.create(counting.this,R.raw.bird);
                    mp.start();
                    updateQuestions(r.nextInt(mQuestionsLength));
                }else{
                    mp=MediaPlayer.create(counting.this,R.raw.april);
                    mp.start();
                }
            }
        });
    }

    private int getImageResource(ImageView iv ){
        return (Integer)iv.getTag();
    }

    private void updateQuestions(int num){
        question.setImageResource(mQuestions.getQuestion(num));
        choice_one.setImageResource(mQuestions.getChoice1(num));
        choice_two.setImageResource(mQuestions.getChoice2(num));
        choice_three.setImageResource(mQuestions.getChoice3(num));
        choice_four.setImageResource(mQuestions.getChoice4(num));

        mAnswer = mQuestions.getAnswer(num);
    } 
}

question class:

public class Questions {
    public int mQuestions[]={
        R.drawable.onee,  R.drawable.twoe,  R.drawable.threee,  R.drawable.foure, R.drawable.sixe, R.drawable.eighte
    };

    public int mChoices[][]={
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_three,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_two,  R.drawable.counting_six,  R.drawable.counting_four},
        {R.drawable.counting_one,  R.drawable.counting_eight,  R.drawable.counting_three,  R.drawable.counting_four}

     };

    public int mAnswers[]=
{R.drawable.counting_one,R.drawable.counting_two,R.drawable.counting_three,
        R.drawable.counting_four, R.drawable.counting_six, R.drawable.counting_eight};

    public int getQuestion(int a){
        int question = mQuestions[a];
        return question;
    }

    public int getChoice1(int a){
        int choice = mChoices[a][0];
        return choice;
    }

    public int getChoice2(int a){
        int choice = mChoices[a][1];
        return choice;
    }

    public int getChoice3(int a){
        int choice = mChoices[a][2];
        return choice;
    }

    public int getChoice4(int a){
        int choice = mChoices[a][3];
        return choice;
    }

    public int getAnswer(int a){
        int answer = mAnswers[a];
        return answer;
    }
}

Solution

  • You are doing wrong comparison in

    getImageResource(choice_one) == mAnswer
    

    You are comparing resource Id with tag that you have provided (1,2,3,4). This is wrong.

    You may do it in this way:-

        private void updateQuestions(int num){
            question.setImageResource(mQuestions.getQuestion(num));
            question. setTag(mQuestions.getQuestion(num));
            choice_one.setImageResource(mQuestions.getChoice1(num));
            choice_one.setTag(mQuestions.getChoice1(num));
            choice_two.setImageResource(mQuestions.getChoice2(num));
            choice_two.setTag(mQuestions.getChoice2(num));
            choice_three.setImageResource(mQuestions.getChoice3(num));
            choice_three.setTag(mQuestions.getChoice3(num));
            choice_four.setImageResource(mQuestions.getChoice4(num));
            choice_four.setTag(mQuestions.getChoice4(num));
    
            mAnswer = mQuestions.getAnswer(num);
        } 
    

    Remove the tags(below code) that you have provided and try

            choice_one.setTag(1);
            choice_two.setTag(2);
            choice_three.setTag(3);
            choice_four.setTag(4);