2017-10-17 98 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)的'值'和'毫無疑問'你期望他們是什麼?輸入一些打印語句來檢查這些是否符合預期。你可能也想打印你的'mAnswers []'數組來交叉引用。 –

+0

我使用setTag for'getImageResource',但我不知道如何從'mAnswer'中檢索值。 – danish

回答

0

你做錯了比較

getImageResource(choice_one) == mAnswer 

您比較資源ID與標籤您所提供(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