55import android .view .View ;
66import android .widget .LinearLayout ;
77
8+ import com .aadyad .checkboxquestion .Views .MultipleAnswerQuestion ;
9+ import com .aadyad .checkboxquestion .Views .MultipleChoiceQuestion ;
10+ import com .aadyad .checkboxquestion .Views .YesOrNoQuestion ;
11+
812import java .util .ArrayList ;
913import java .util .Arrays ;
14+ import java .util .Collections ;
1015
1116public class QuestionList {
1217
@@ -39,27 +44,33 @@ public void createQuestionViews(){
3944 linearLayout .setOrientation (LinearLayout .VERTICAL );
4045 if (questions == null ){
4146 for (Question q : choiceQuestions ){
42- if (q .type .equals (Question .MULTIPLE_CHOICE_QUESTION )) {
43- Log .d ("TAG" , "createQuestionViews: " + Arrays .toString (q .options ));
44- MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion (context );
45- multipleChoiceQuestion .init (q .question , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize (), settings .getQuestionLayoutHeight (), settings .getOptionLayoutHeight (), q .options );
46- linearLayout .addView (multipleChoiceQuestion );
47- }else if (q .type .equals (Question .YES_OR_NO_QUESTION )){
48- YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion (context );
49- yesOrNoQuestion .init (q .question , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize (), settings .getQuestionLayoutHeight (), settings .getOptionLayoutHeight ());
50- linearLayout .addView (yesOrNoQuestion );
51- } else if (q .type .equals (Question .MULTIPLE_ANSWER_QUESTION )){
52- //Todo: Add code for multipleanswerquestion
53- } else {
54- throw new RuntimeException ("Type " + q .type + " does not exist." );
47+ switch (q .type ) {
48+ case Question .MULTIPLE_CHOICE_QUESTION :
49+ Log .d ("TAG" , "createQuestionViews: " + Arrays .toString (q .options ));
50+ MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion (context );
51+ multipleChoiceQuestion .init (q .question , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize (), q .options );
52+ linearLayout .addView (multipleChoiceQuestion );
53+ break ;
54+ case Question .YES_OR_NO_QUESTION :
55+ YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion (context );
56+ yesOrNoQuestion .init (q .question , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize ());
57+ linearLayout .addView (yesOrNoQuestion );
58+ break ;
59+ case Question .MULTIPLE_ANSWER_QUESTION :
60+ MultipleAnswerQuestion multipleAnswerQuestion = new MultipleAnswerQuestion (context );
61+ multipleAnswerQuestion .init (q .question , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize (), q .options );
62+ linearLayout .addView (multipleAnswerQuestion );
63+ break ;
64+ default :
65+ throw new RuntimeException ("Type " + q .type + " does not exist." );
5566 }
5667 i ++;
5768 }
5869 } else {
5970 for (String q : questions ) {
6071 i ++;
6172 YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion (context );
62- yesOrNoQuestion .init (q , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize (), settings . getQuestionLayoutHeight (), settings . getOptionLayoutHeight () );
73+ yesOrNoQuestion .init (q , String .valueOf (i ), settings .isNumEnabled (), settings .getSpacing (), this .orientation , settings .getCheckBoxLocation (), settings .getQuestionTextSize (), settings .getCheckBoxTextSize ());
6374 linearLayout .addView (yesOrNoQuestion );
6475 }
6576 }
@@ -69,19 +80,29 @@ public void createQuestionViews(){
6980 public void setLayoutOrientation (int orientation ){
7081 this .orientation = orientation ;
7182 for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
72- YesOrNoQuestion yesOrNoQuestion = null ;
73- MultipleChoiceQuestion multipleChoiceQuestion = null ;
74- View v = linearLayout .getChildAt (i );
83+ YesOrNoQuestion yesOrNoQuestion ;
84+ MultipleChoiceQuestion multipleChoiceQuestion ;
85+ MultipleAnswerQuestion multipleAnswerQuestion ;
86+
7587 try {
76- yesOrNoQuestion = (YesOrNoQuestion ) v ;
77- } catch (ClassCastException e ) {
78- multipleChoiceQuestion = (MultipleChoiceQuestion ) v ;
79- }
80- if (yesOrNoQuestion != null ){
88+ yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (i );
8189 yesOrNoQuestion .setCheckboxOrientation (orientation );
82- }else {
83- assert multipleChoiceQuestion != null ;
90+ } catch (ClassCastException ignored ) {
91+
92+ }
93+
94+ try {
95+ multipleChoiceQuestion = (MultipleChoiceQuestion ) getQuestion (i );
8496 multipleChoiceQuestion .setCheckboxOrientation (orientation );
97+ } catch (ClassCastException ignored ) {
98+
99+ }
100+
101+ try {
102+ multipleAnswerQuestion = (MultipleAnswerQuestion ) getQuestion (i );
103+ multipleAnswerQuestion .setCheckboxOrientation (orientation );
104+ } catch (ClassCastException ignored ) {
105+
85106 }
86107 }
87108 createQuestionViews ();
@@ -91,15 +112,12 @@ public float getPercentageOfCorrectAnswers(){
91112 int correctAnswers = 0 ;
92113 int allAnswers = linearLayout .getChildCount ();
93114 for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
94- YesOrNoQuestion yesOrNoQuestion = null ;
95- MultipleChoiceQuestion multipleChoiceQuestion = null ;
96- View v = linearLayout .getChildAt (i );
115+ YesOrNoQuestion yesOrNoQuestion ;
116+ MultipleChoiceQuestion multipleChoiceQuestion ;
117+ MultipleAnswerQuestion multipleAnswerQuestion ;
118+
97119 try {
98- yesOrNoQuestion = (YesOrNoQuestion ) v ;
99- } catch (ClassCastException e ) {
100- multipleChoiceQuestion = (MultipleChoiceQuestion ) v ;
101- }
102- if (yesOrNoQuestion != null ){
120+ yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (i );
103121 int answer = yesOrNoQuestion .getAnswer ();
104122 Log .d ("TAG" , "answer: " + answer );
105123 int correctAnswer = choiceQuestions .get (i ).correctAnswer ;
@@ -109,7 +127,12 @@ public float getPercentageOfCorrectAnswers(){
109127 } else if (answer == correctAnswer ){
110128 correctAnswers ++;
111129 }
112- }else {
130+ } catch (ClassCastException ignored ) {
131+
132+ }
133+
134+ try {
135+ multipleChoiceQuestion = (MultipleChoiceQuestion ) getQuestion (i );
113136 int answer = multipleChoiceQuestion .getAnswer ();
114137 Log .d ("TAG" , "answer: " + answer );
115138 int correctAnswer = choiceQuestions .get (i ).correctAnswer ;
@@ -119,54 +142,95 @@ public float getPercentageOfCorrectAnswers(){
119142 } else if (answer == correctAnswer ){
120143 correctAnswers ++;
121144 }
145+ } catch (Exception ignored ) {
146+
147+ }
148+
149+ try {
150+ multipleAnswerQuestion = (MultipleAnswerQuestion ) getQuestion (i );
151+ ArrayList <Integer > answer = multipleAnswerQuestion .getAnswer ();
152+ Collections .sort (answer );
153+ Log .d ("TAG" , "answer: " + answer );
154+ ArrayList <Integer > correctAnswer = choiceQuestions .get (i ).multipleCorrectAnswer ;
155+ Collections .sort (correctAnswer );
156+ Log .d ("TAG" , "correct answer: " + correctAnswer );
157+ if (correctAnswer .size () == 0 || correctAnswer == null ){
158+ allAnswers --;
159+ } else if (answer .equals (correctAnswer )){
160+ correctAnswers ++;
161+ }
162+ } catch (ClassCastException ignored ) {
163+
122164 }
123165 }
124166 Log .d ("TAG" , "getPercentageOfCorrectAnswers: " + correctAnswers );
125167 Log .d ("TAG" , "getPercentageOfCorrectAnswers: " + allAnswers );
126168 return (float ) correctAnswers /allAnswers ;
127169 }
128170
129- public ArrayList <Integer > getAnswers (){
130- ArrayList <Integer > answers = new ArrayList <>();
171+ public ArrayList <Object > getAnswers (){
172+ ArrayList <Object > answers = new ArrayList <>();
131173 Log .d ("answers" , "list size: " + linearLayout .getChildCount ());
132174 for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
133- YesOrNoQuestion yesOrNoQuestion = null ;
134- MultipleChoiceQuestion multipleChoiceQuestion = null ;
135- View v = linearLayout .getChildAt (i );
175+ YesOrNoQuestion yesOrNoQuestion ;
176+ MultipleChoiceQuestion multipleChoiceQuestion ;
177+ MultipleAnswerQuestion multipleAnswerQuestion ;
178+
136179 try {
137- yesOrNoQuestion = (YesOrNoQuestion ) v ;
138- } catch (ClassCastException e ) {
139- multipleChoiceQuestion = (MultipleChoiceQuestion ) v ;
140- }
141- if (yesOrNoQuestion != null ){
180+ yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (i );
142181 answers .add (yesOrNoQuestion .getAnswer ());
143- }else {
182+ } catch (ClassCastException ignored ) {
183+
184+ }
185+
186+ try {
187+ multipleChoiceQuestion = (MultipleChoiceQuestion ) getQuestion (i );
144188 answers .add (multipleChoiceQuestion .getAnswer ());
189+ } catch (Exception ignored ) {
190+
191+ }
192+
193+ try {
194+ multipleAnswerQuestion = (MultipleAnswerQuestion ) getQuestion (i );
195+ answers .add (multipleAnswerQuestion .getAnswer ());
196+ } catch (Exception ignored ) {
197+
145198 }
146199 }
147200 return answers ;
148201 }
149202
150203 public boolean areAllQuestionsAnswered (){
151- ArrayList <Integer > answers = new ArrayList <>();
152204 Log .d ("answers" , "list size: " + linearLayout .getChildCount ());
153205 for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
154- YesOrNoQuestion yesOrNoQuestion = null ;
155- MultipleChoiceQuestion multipleChoiceQuestion = null ;
156- View v = linearLayout .getChildAt (i );
206+ YesOrNoQuestion yesOrNoQuestion ;
207+ MultipleChoiceQuestion multipleChoiceQuestion ;
208+ MultipleAnswerQuestion multipleAnswerQuestion ;
209+
157210 try {
158- yesOrNoQuestion = (YesOrNoQuestion ) v ;
159- } catch (ClassCastException e ) {
160- multipleChoiceQuestion = (MultipleChoiceQuestion ) v ;
161- }
162- if (yesOrNoQuestion != null ){
211+ yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (i );
163212 if (yesOrNoQuestion .getAnswer () == 0 ){
164213 return false ;
165214 }
166- }else {
215+ } catch (ClassCastException ignored ) {
216+
217+ }
218+ try {
219+ multipleChoiceQuestion = (MultipleChoiceQuestion ) getQuestion (i );
167220 if (multipleChoiceQuestion .getAnswer () == 0 ){
168221 return false ;
169222 }
223+ } catch (Exception ignored ) {
224+
225+ }
226+
227+ try {
228+ multipleAnswerQuestion = (MultipleAnswerQuestion ) getQuestion (i );
229+ if (multipleAnswerQuestion .getAnswer ().size () == 0 ){
230+ return false ;
231+ }
232+ } catch (Exception ignored ) {
233+
170234 }
171235 }
172236 return true ;
@@ -175,24 +239,42 @@ public boolean areAllQuestionsAnswered(){
175239 public View getQuestion (int index ){
176240 YesOrNoQuestion yesOrNoQuestion = null ;
177241 MultipleChoiceQuestion multipleChoiceQuestion = null ;
242+ MultipleAnswerQuestion multipleAnswerQuestion = null ;
178243 View v = linearLayout .getChildAt (index );
179244 try {
180245 yesOrNoQuestion = (YesOrNoQuestion ) v ;
181- } catch (ClassCastException e ) {
246+ } catch (ClassCastException ignored ) {
247+
248+ }
249+
250+ try {
182251 multipleChoiceQuestion = (MultipleChoiceQuestion ) v ;
252+ } catch (Exception ignored ) {
253+
183254 }
255+
256+ try {
257+ multipleAnswerQuestion = (MultipleAnswerQuestion ) v ;
258+ } catch (Exception ignored ) {
259+
260+ }
261+
184262 if (yesOrNoQuestion != null ){
185263 return yesOrNoQuestion ;
186- }else {
264+ }else if ( multipleChoiceQuestion != null ) {
187265 return multipleChoiceQuestion ;
266+ } else if (multipleAnswerQuestion != null ){
267+ return multipleAnswerQuestion ;
268+ }else {
269+ return null ;
188270 }
189271 }
190272
191273 public void addOnValueChangedRunnable (int index , Runnable r ){
192274 try {
193275 ((MultipleChoiceQuestion ) getQuestion (index )).doOnValueChanged (r );
194276 } catch (Exception e ) {
195- YesOrNoQuestion yesOrNoQuestion = (YesOrNoQuestion ) linearLayout . getChildAt (index );
277+ YesOrNoQuestion yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (index );
196278 //Todo: add do on value changed
197279 }
198280 }
0 commit comments