@@ -22,28 +22,28 @@ public class QuestionList {
22
22
Context context ;
23
23
int orientation ;
24
24
25
- public QuestionList (ArrayList <Question > questions , QuestionListSettings settings , Context context ){
25
+ public QuestionList (ArrayList <Question > questions , QuestionListSettings settings , Context context ) {
26
26
choiceQuestions = questions ;
27
27
this .settings = settings ;
28
28
this .context = context ;
29
29
this .orientation = settings .getCheckBoxOrientation ();
30
30
linearLayout = new LinearLayout (context );
31
31
}
32
32
33
- public QuestionList (String [] questions , QuestionListSettings settings , Context context ){
33
+ public QuestionList (String [] questions , QuestionListSettings settings , Context context ) {
34
34
this .questions = questions ;
35
35
this .settings = settings ;
36
36
this .orientation = settings .getCheckBoxOrientation ();
37
37
this .context = context ;
38
38
linearLayout = new LinearLayout (context );
39
39
}
40
40
41
- public void createQuestionViews (){
41
+ public void createQuestionViews () {
42
42
int i = 1 ;
43
43
linearLayout = new LinearLayout (context );
44
44
linearLayout .setOrientation (LinearLayout .VERTICAL );
45
- if (questions == null ){
46
- for (Question q : choiceQuestions ){
45
+ if (questions == null ) {
46
+ for (Question q : choiceQuestions ) {
47
47
switch (q .type ) {
48
48
case Question .MULTIPLE_CHOICE_QUESTION :
49
49
Log .d ("TAG" , "createQuestionViews: " + Arrays .toString (q .options ));
@@ -77,7 +77,7 @@ public void createQuestionViews(){
77
77
78
78
}
79
79
80
- public void setLayoutOrientation (int orientation ){
80
+ public void setLayoutOrientation (int orientation ) {
81
81
this .orientation = orientation ;
82
82
for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
83
83
YesOrNoQuestion yesOrNoQuestion ;
@@ -108,7 +108,7 @@ public void setLayoutOrientation(int orientation){
108
108
createQuestionViews ();
109
109
}
110
110
111
- public float getPercentageOfCorrectAnswers (){
111
+ public float getPercentageOfCorrectAnswers () {
112
112
int correctAnswers = 0 ;
113
113
int allAnswers = linearLayout .getChildCount ();
114
114
for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
@@ -122,9 +122,9 @@ public float getPercentageOfCorrectAnswers(){
122
122
Log .d ("TAG" , "answer: " + answer );
123
123
int correctAnswer = choiceQuestions .get (i ).correctAnswer ;
124
124
Log .d ("TAG" , "correct answer: " + correctAnswer );
125
- if (correctAnswer == 0 ){
125
+ if (correctAnswer == 0 ) {
126
126
allAnswers --;
127
- } else if (answer == correctAnswer ){
127
+ } else if (answer == correctAnswer ) {
128
128
correctAnswers ++;
129
129
}
130
130
} catch (ClassCastException ignored ) {
@@ -137,9 +137,9 @@ public float getPercentageOfCorrectAnswers(){
137
137
Log .d ("TAG" , "answer: " + answer );
138
138
int correctAnswer = choiceQuestions .get (i ).correctAnswer ;
139
139
Log .d ("TAG" , "correct answer: " + correctAnswer );
140
- if (correctAnswer == 0 ){
140
+ if (correctAnswer == 0 ) {
141
141
allAnswers --;
142
- } else if (answer == correctAnswer ){
142
+ } else if (answer == correctAnswer ) {
143
143
correctAnswers ++;
144
144
}
145
145
} catch (Exception ignored ) {
@@ -154,9 +154,9 @@ public float getPercentageOfCorrectAnswers(){
154
154
ArrayList <Integer > correctAnswer = choiceQuestions .get (i ).multipleCorrectAnswer ;
155
155
Collections .sort (correctAnswer );
156
156
Log .d ("TAG" , "correct answer: " + correctAnswer );
157
- if (correctAnswer .size () == 0 || correctAnswer == null ){
157
+ if (correctAnswer .size () == 0 || correctAnswer == null ) {
158
158
allAnswers --;
159
- } else if (answer .equals (correctAnswer )){
159
+ } else if (answer .equals (correctAnswer )) {
160
160
correctAnswers ++;
161
161
}
162
162
} catch (ClassCastException ignored ) {
@@ -165,10 +165,10 @@ public float getPercentageOfCorrectAnswers(){
165
165
}
166
166
Log .d ("TAG" , "getPercentageOfCorrectAnswers: " + correctAnswers );
167
167
Log .d ("TAG" , "getPercentageOfCorrectAnswers: " + allAnswers );
168
- return (float ) correctAnswers / allAnswers ;
168
+ return (float ) correctAnswers / allAnswers ;
169
169
}
170
170
171
- public ArrayList <Object > getAnswers (){
171
+ public ArrayList <Object > getAnswers () {
172
172
ArrayList <Object > answers = new ArrayList <>();
173
173
Log .d ("answers" , "list size: " + linearLayout .getChildCount ());
174
174
for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
@@ -200,7 +200,7 @@ public ArrayList<Object> getAnswers(){
200
200
return answers ;
201
201
}
202
202
203
- public boolean areAllQuestionsAnswered (){
203
+ public boolean areAllQuestionsAnswered () {
204
204
Log .d ("answers" , "list size: " + linearLayout .getChildCount ());
205
205
for (int i = 0 ; i < linearLayout .getChildCount (); i ++) {
206
206
YesOrNoQuestion yesOrNoQuestion ;
@@ -209,15 +209,15 @@ public boolean areAllQuestionsAnswered(){
209
209
210
210
try {
211
211
yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (i );
212
- if (yesOrNoQuestion .getAnswer () == 0 ){
212
+ if (yesOrNoQuestion .getAnswer () == 0 ) {
213
213
return false ;
214
214
}
215
215
} catch (ClassCastException ignored ) {
216
216
217
217
}
218
218
try {
219
219
multipleChoiceQuestion = (MultipleChoiceQuestion ) getQuestion (i );
220
- if (multipleChoiceQuestion .getAnswer () == 0 ){
220
+ if (multipleChoiceQuestion .getAnswer () == 0 ) {
221
221
return false ;
222
222
}
223
223
} catch (Exception ignored ) {
@@ -226,7 +226,7 @@ public boolean areAllQuestionsAnswered(){
226
226
227
227
try {
228
228
multipleAnswerQuestion = (MultipleAnswerQuestion ) getQuestion (i );
229
- if (multipleAnswerQuestion .getAnswer ().size () == 0 ){
229
+ if (multipleAnswerQuestion .getAnswer ().size () == 0 ) {
230
230
return false ;
231
231
}
232
232
} catch (Exception ignored ) {
@@ -236,7 +236,7 @@ public boolean areAllQuestionsAnswered(){
236
236
return true ;
237
237
}
238
238
239
- public View getQuestion (int index ){
239
+ public View getQuestion (int index ) {
240
240
YesOrNoQuestion yesOrNoQuestion = null ;
241
241
MultipleChoiceQuestion multipleChoiceQuestion = null ;
242
242
MultipleAnswerQuestion multipleAnswerQuestion = null ;
@@ -259,27 +259,38 @@ public View getQuestion(int index){
259
259
260
260
}
261
261
262
- if (yesOrNoQuestion != null ){
262
+ if (yesOrNoQuestion != null ) {
263
263
return yesOrNoQuestion ;
264
- }else if (multipleChoiceQuestion != null ){
264
+ } else if (multipleChoiceQuestion != null ) {
265
265
return multipleChoiceQuestion ;
266
- } else if (multipleAnswerQuestion != null ){
266
+ } else if (multipleAnswerQuestion != null ) {
267
267
return multipleAnswerQuestion ;
268
- }else {
268
+ } else {
269
269
return null ;
270
270
}
271
271
}
272
272
273
- public void addOnValueChangedRunnable (int index , Runnable r ){
273
+ public void addOnAnswerChangedListener (int index , OnAnswerChangedListener r ) {
274
274
try {
275
- ((MultipleChoiceQuestion ) getQuestion (index )).doOnValueChanged (r );
276
- } catch (Exception e ) {
277
- YesOrNoQuestion yesOrNoQuestion = (YesOrNoQuestion ) getQuestion (index );
278
- //Todo: add do on value changed
275
+ ((MultipleChoiceQuestion ) getQuestion (index )).addOnAnswerChangedListener (r );
276
+ } catch (Exception ignored ) {
277
+
278
+ }
279
+
280
+ try {
281
+ ((MultipleAnswerQuestion ) getQuestion (index )).addOnAnswerChangedListener (r );
282
+ } catch (Exception ignored ) {
283
+
284
+ }
285
+
286
+ try {
287
+ ((YesOrNoQuestion ) getQuestion (index )).addOnAnswerChangedListener (r );
288
+ } catch (Exception ignored ) {
289
+
279
290
}
280
291
}
281
292
282
- public LinearLayout getQuestionViews (){
293
+ public LinearLayout getQuestionViews () {
283
294
return linearLayout ;
284
295
}
285
296
}
0 commit comments