2014-02-14 2 views
0

아래에서 알 수 있듯이 저는 여러 유형의 질문 (RadioButtons 및 CheckBoxes)을 사용하여 일종의 퀴즈를 동적으로 생성하고 있습니다.
그래서 나는 그것들을 올바르게 생성 할 수 있습니다.
질문 끝에 정상적인 버튼이 있습니다.
그리고 나는 모든 질문에 답을 얻은 후에 만 ​​클릭 가능하게하고 싶습니다.
모든 RadioButton에 응답 할 수있는 기능이 있습니다.
하지만 두 가지 유형의 버튼이 있으므로 CheckBox 답변의 질문에 적어도 1 개의 확인 된 답변이 있는지 확인하는 방법을 알 수 없습니다. 당신은 예를 들어 (i*10)+zed에 체크 박스의 ID를 설정 질문 미만 10 개 답변이있을 경우Android 퀴즈 애플리케이션 - 모든 질문에 답변 함을 확인하십시오.

ArrayList<RadioGroup> arrGroup; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 
    buttHole = (Button) findViewById(R.id.button1); 
    buttHole.setEnabled(false);  

    getQuestions(); 

    //Creating the list of Questions 
    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1); 
    arrGroup = new ArrayList<RadioGroup>(); 
    try 
    { 

     for (int i = 0; i <= question.length; i++) 
     { 
      if(question[i].multichoice == true) 
      { 
       TextView title2 = new TextView(this); 
       title2.setText(question[i].questionName); 
       title2.setTextColor(Color.BLACK); 
       mLinearLayout.addView(title2); 

       for(int zed = 0; zed < question[i].answers.length; zed++) 
       {    
        CheckBox box = new CheckBox(this); 
        box.setText(question[i].answers[zed].answer); 
        box.setId(zed); 
        mLinearLayout.addView(box); 
       } 
      } 
      else 
      { 
       // create text button 
       TextView title = new TextView(this); 
       title.setText(question[i].questionName);     
       title.setTextColor(Color.BLACK); 
       mLinearLayout.addView(title); 

       // create radio button 
       final RadioButton[] rb = new RadioButton[question[i].answers.length]; 
       RadioGroup radGr = new RadioGroup(this); 
       // take a reference of RadioGroup view 
       arrGroup.add(radGr);   

       radGr.setOrientation(RadioGroup.VERTICAL); 
       radGr.setOnClickListener(new OnClickListener() 
       { 
        @Override 
        public void onClick(View v) 
        {     
         checkQuestionsAnswer(); 
        } 

       }); 

       for (int j = 0; j < question[i].answers.length; j++) { 
        rb[j] = new RadioButton(this); 
        radGr.addView(rb[j]); 
        rb[j].setText(question[i].answers[j].answer); 
       } 
       mLinearLayout.addView(radGr); 
      } 
     } 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    }  

} 

public boolean checkQuestionsAnswer() 
{ 
    int count = arrGroup.size(); 

    for (int i = 0; i < count; i++) 
    { 
     if (arrGroup.get(i).getCheckedRadioButtonId() == -1) 
     { 
      Toast.makeText(getApplicationContext(), "Not all questions are answered!", Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    } 
    Toast.makeText(getApplicationContext(), "Thank you!", Toast.LENGTH_SHORT).show(); 
    buttHole.setEnabled(true); 
    //Return true if all answer are given 
    return true; 
} 

답변

0

:

나는 다음과 같은 코드가 있습니다. 당신이 할 수이 방법이

for(int i=0; i<question.length; i++){ 

    boolean questionAnswered = false; 
    for(int zed = 0; zed < question[i].answers.length; zed++) { 

     CheckBox box = (CheckBox) findViewById(i*10+zed); 
     if(box != null && box.isChecked()) { 

      questionAnswered = true; 
      ... 
     }   

    } 

} 

같은 대답을 반복이 코드를 시도하지 않았고, 그것은 단지 신속하고 더러운 힌트,하지만 난 그게 도움이되기를 바랍니다.

+0

아무 이유없이 작동하지 않았 음 :/확실하지 않은 이유가 – user3182266

+0

어디에서 디버깅 할 수 있습니까? 정확히 무슨 일을하지 않았습니까? –

관련 문제