2017-10-17 2 views
0

私は質問として画像を使用していて、ここで私は配列から画像を比較する方法がわからないという多項選択クイズアプリを開発しています。 onclickメソッドでは、配列に格納された正しい答えとクリックされた選択肢を比較していますが、それでも私はsetTaggetTagの選択肢を使用しています。どのように私は画像を比較することができます

しないでください。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

私はsetTagを 'getImageResource'に使用していますが、' mAnswer'から値を取得する方法はわかりません。 – danish

答えて

0

、あなたが提供しているそのタグでリソースIdを比較している

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

関連する問題