2017-10-17 3 views
0

나는 이미지를 질문으로 사용하고 객관식 퀴즈 앱을 개발 중입니다. 여기서 문제는 내가 배열에서 이미지를 비교하는 방법을 모른다는 것입니다. onclick 메서드에서 배열에 저장된 올바른 대답을 클릭 한 선택 비교하지만 해당 작동하지 심지어 setTag 및 선택에 대한 getTag 방법은 내가 무엇을 시도한 코드가 잘못되었습니다.어떻게 이미지를 비교할 수 있습니까

제발 대답 downvote 질문에 대답하고 싶지 않으면.

여기 내 코드입니다.

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); 
    } 
} 

질문 클래스 :

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; 
    } 
} 
+0

이 getImageResource (choice_num)'의 값 당신이 제공 한 그 (코드 아래) 태그를 제거하고 시도 '와 'mAnswer' 당신이 기대하는 바는 무엇입니까? 이들이 모두 예상대로 작동하는지 확인하기 위해 인쇄 문을 넣으십시오. 'mAnswers []'배열을 상호 참조로 인쇄 할 수도 있습니다. –

+0

'getImageResource'에 setTag를 사용하지만'mAnswer'에서 값을 가져 오는 법을 모르겠습니다. – danish

답변

0

당신은 당신이 제공 한 해당 태그와 자원 아이디를 비교하는

getImageResource(choice_one) == mAnswer 

에 잘못된 비교를하고있다 (1,2,3,4) . 이것은 잘못된 것입니다.

당신은이 방법으로 그것을 할 수 있습니다 : -

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); 
    } 

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

작동합니다. 너 정말 대단해. 고마워. – danish

관련 문제