2016-04-24 9 views
1

私は質問と回答の配列を手作業で書いて、ラジオボタンとテキストビューのテキスト値を設定するためのクイズアプリを開発しようとしています。しかし、私はそれを実行し、それを再生しようとすると、質問の最初のセットでは、次に正解は正解が不正確になり、その逆も成り立ちました。以下のスクリーンショット答えが正しいときに間違っているAndroidクイズアプリ

正しいものを押したときの正しいもの。

​​

間違った1が、私は正しいものを(あなたが答えを知っている)を押しました。

enter image description here

質問の値の最初のセットは移動していなかったように私には思えます。私は考えることができるすべてを試みたが、運はなかった。私は現在のテキストを印刷しようとしたときに私のログキャットで何が起こるかを見ます。ここに私のlogcatからスニペットです。

04-24 01:56:10.880 4093-4093/com.example.muhammad.exquizme D/answer: Google 
04-24 01:56:10.880 4093-4093/com.example.muhammad.exquizme D/correctAnswer: Google 
04-24 01:56:10.885 4093-4093/com.example.muhammad.exquizme D/Is it correct?: Correct 
04-24 01:56:10.894 4093-4147/com.example.muhammad.exquizme W/OpenGLRenderer: Fail to change FontRenderer cache size, it already initialized 
04-24 01:56:32.322 4093-4093/com.example.muhammad.exquizme D/answer: Google 
04-24 01:56:32.322 4093-4093/com.example.muhammad.exquizme D/correctAnswer: An algorithm 
04-24 01:56:32.326 4093-4093/com.example.muhammad.exquizme D/Is it correct?: Incorrect 

ここにその部分のコードを示します。

QuizQuestion[] questionArray; //global variable 
    int randomIndex;//global variable 

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

     qIndexView = (TextView) findViewById(R.id.currentNumber); 
     questionTextView = (TextView) findViewById(R.id.questionTextView); 
     choiceBtnA = (RadioButton) findViewById(R.id.choiceA); 
     choiceBtnB = (RadioButton) findViewById(R.id.choiceB); 
     choiceBtnC = (RadioButton) findViewById(R.id.choiceC); 
     choiceBtnD = (RadioButton) findViewById(R.id.choiceD); 
     questionArray = new QuizQuestion[5]; 

     displayQuestion(); 


     final String a = choiceBtnA.getText().toString(); 
     final String b = choiceBtnB.getText().toString(); 
     final String c = choiceBtnC.getText().toString(); 
     final String d = choiceBtnD.getText().toString(); 

     choiceBtnA.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       checkAnswers(a); 
       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         displayQuestion(); 
        } 
       }, 2000); 

       //displayQuestion(); 
      } 
     }); 

     choiceBtnB.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       checkAnswers(b); 
       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         displayQuestion(); 
        } 
       }, 2000); 
      } 
     }); 

     choiceBtnC.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       checkAnswers(c); 
       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         displayQuestion(); 
        } 
       }, 2000); 
      } 
     }); 

     choiceBtnD.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       checkAnswers(d); 
       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         displayQuestion(); 
        } 
       }, 2000); 
      } 
     }); 
    } 

//This is called every pressed 
public void displayQuestion() { 

     //Create Question list 
     String theQuestion = "Android mobile operating system was released on 23rd September 2008 developed by who?"; 
     String[] choices = new String[]{"Google", "IBM", "Intel", "Oracle"}; 
     questionArray[0] = new QuizQuestion(theQuestion, choices, choices[0], "Computing History"); 

     theQuestion = "Who invented the programming language called 'Java'?"; 
     choices = new String[]{"James Gosling", "Steve Jobs", "Bill Gates", "Elon Musk"}; 
     questionArray[1] = new QuizQuestion(theQuestion, choices, choices[0], "Computing History"); 

     theQuestion = "Which of the following languages is more suited to a structured program?"; 
     choices = new String[]{"FORTRAN", "BASIC", "PASCAL", "None of the above"}; 
     questionArray[2] = new QuizQuestion(theQuestion, choices, choices[3], "Computer Fundamentals"); 

     theQuestion = "The brain of any computer system is"; 
     choices = new String[]{"Memory", "ALU", "CPU", "Control unit"}; 
     questionArray[3] = new QuizQuestion(theQuestion, choices, choices[2], "Computer Fundamentals"); 

     theQuestion = "The step-by-step instructions that solve a problem are called _____."; 
     choices = new String[]{"An algorithm", "A list", "A plan", "A sequential structure"}; 
     questionArray[4] = new QuizQuestion(theQuestion, choices, choices[0], "System Analysis and Design"); 

     randomIndex = new Random().nextInt(questionArray.length); 

     questionTextView.setText(questionArray[randomIndex].question); 
     choiceBtnA.setText(questionArray[randomIndex].choices[0]); 
     choiceBtnB.setText(questionArray[randomIndex].choices[1]); 
     choiceBtnC.setText(questionArray[randomIndex].choices[2]); 
     choiceBtnD.setText(questionArray[randomIndex].choices[3]); 

    } 

//checks answer when clicked 
public boolean checkAnswers(String answer) { 
     Log.d("answer", answer); 
     String correctAnswer = questionArray[randomIndex].answer; 
     Log.d("correctAnswer", correctAnswer); 
     if (answer.equals(correctAnswer)) { 
      Toast.makeText(PlayQuizActivity.this, "Correct", Toast.LENGTH_SHORT).show(); 
      Log.d("Is it correct?", "Correct"); 
      return true; 
     } else { 
      Toast.makeText(PlayQuizActivity.this, "Incorrect", Toast.LENGTH_SHORT).show(); 
      Log.d("Is it correct?", "Incorrect"); 
     } 

     return false; 
} 

これはすべて私が現在持っているものです。あなたが私の英語がそれほど良いものではないことを明確にする必要があるかどうかを教えてください。前もって感謝します。

+1

'checkAnswers()'に 'String'がどのように渡されているかを表示してください。 –

+0

@MikeM更新しました。今見てみましょう。ありがとう。 –

+1

クリックすると 'Button'sの現在のテキストを取得する必要があります。 'checkAnswers(choiceBtnA.getText()。toString());' checkAnswers(choiceBtnB.getText()。toString()); 'に' onClick() 'メソッドの' checkAnswers() 'への呼び出しを' –

答えて

1

最初の質問を表示した後、答えとしてButtonの最初のテキストを保存していますが、次の質問に移動すると更新されません。

ここをクリックしてButtonの現在のテキストを取得する必要があります。また

など、 checkAnswers(choiceBtnB.getText().toString());checkAnswers(choiceBtnA.getText().toString());onClick()方法で checkAnswers()への呼び出しを変更して、あなたの代わりに questionArrayから答え Stringを得ることができます。例えば、 checkAnswers(questionArray[randomIndex].choices[0]);checkAnswers(questionArray[randomIndex].choices[1]);など

はさらに、あなたは少し代わりcheckAnswers()Buttonのインデックスを表すintを渡すことで、コードをダウントリムができ、そこに選択した答えをつかみます。

choiceBtnA.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     checkAnswers(0); 
     ... 
    } 
}); 

... 

public boolean checkAnswers(int index) { 
    String answer = questionArray[randomIndex].choices[index]; 
    String correctAnswer = questionArray[randomIndex].answer; 
    ... 
} 
+1

恐ろしい...ありがとう.... –

関連する問題