Skip to content

Commit 63d964b

Browse files
committed
Renamed methods and added OnAnswerChangedListener, added OnAnswerChangedListener for yesornoquestion as well.
1 parent 57d22cb commit 63d964b

File tree

6 files changed

+222
-132
lines changed

6 files changed

+222
-132
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.aadyad.checkboxquestion;
2+
3+
import java.util.ArrayList;
4+
5+
public interface OnAnswerChangedListener {
6+
public void onAnswerChanged(int answer);
7+
public void onAnswerChanged(ArrayList<Integer> answer);
8+
}

CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/QuestionList.java

+41-30
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ public class QuestionList {
2222
Context context;
2323
int orientation;
2424

25-
public QuestionList(ArrayList<Question> questions, QuestionListSettings settings, Context context){
25+
public QuestionList(ArrayList<Question> questions, QuestionListSettings settings, Context context) {
2626
choiceQuestions = questions;
2727
this.settings = settings;
2828
this.context = context;
2929
this.orientation = settings.getCheckBoxOrientation();
3030
linearLayout = new LinearLayout(context);
3131
}
3232

33-
public QuestionList(String[] questions, QuestionListSettings settings, Context context){
33+
public QuestionList(String[] questions, QuestionListSettings settings, Context context) {
3434
this.questions = questions;
3535
this.settings = settings;
3636
this.orientation = settings.getCheckBoxOrientation();
3737
this.context = context;
3838
linearLayout = new LinearLayout(context);
3939
}
4040

41-
public void createQuestionViews(){
41+
public void createQuestionViews() {
4242
int i = 1;
4343
linearLayout = new LinearLayout(context);
4444
linearLayout.setOrientation(LinearLayout.VERTICAL);
45-
if (questions == null){
46-
for (Question q : choiceQuestions){
45+
if (questions == null) {
46+
for (Question q : choiceQuestions) {
4747
switch (q.type) {
4848
case Question.MULTIPLE_CHOICE_QUESTION:
4949
Log.d("TAG", "createQuestionViews: " + Arrays.toString(q.options));
@@ -77,7 +77,7 @@ public void createQuestionViews(){
7777

7878
}
7979

80-
public void setLayoutOrientation(int orientation){
80+
public void setLayoutOrientation(int orientation) {
8181
this.orientation = orientation;
8282
for (int i = 0; i < linearLayout.getChildCount(); i++) {
8383
YesOrNoQuestion yesOrNoQuestion;
@@ -108,7 +108,7 @@ public void setLayoutOrientation(int orientation){
108108
createQuestionViews();
109109
}
110110

111-
public float getPercentageOfCorrectAnswers(){
111+
public float getPercentageOfCorrectAnswers() {
112112
int correctAnswers = 0;
113113
int allAnswers = linearLayout.getChildCount();
114114
for (int i = 0; i < linearLayout.getChildCount(); i++) {
@@ -122,9 +122,9 @@ public float getPercentageOfCorrectAnswers(){
122122
Log.d("TAG", "answer: " + answer);
123123
int correctAnswer = choiceQuestions.get(i).correctAnswer;
124124
Log.d("TAG", "correct answer: " + correctAnswer);
125-
if (correctAnswer == 0){
125+
if (correctAnswer == 0) {
126126
allAnswers--;
127-
} else if (answer == correctAnswer){
127+
} else if (answer == correctAnswer) {
128128
correctAnswers++;
129129
}
130130
} catch (ClassCastException ignored) {
@@ -137,9 +137,9 @@ public float getPercentageOfCorrectAnswers(){
137137
Log.d("TAG", "answer: " + answer);
138138
int correctAnswer = choiceQuestions.get(i).correctAnswer;
139139
Log.d("TAG", "correct answer: " + correctAnswer);
140-
if (correctAnswer == 0){
140+
if (correctAnswer == 0) {
141141
allAnswers--;
142-
} else if (answer == correctAnswer){
142+
} else if (answer == correctAnswer) {
143143
correctAnswers++;
144144
}
145145
} catch (Exception ignored) {
@@ -154,9 +154,9 @@ public float getPercentageOfCorrectAnswers(){
154154
ArrayList<Integer> correctAnswer = choiceQuestions.get(i).multipleCorrectAnswer;
155155
Collections.sort(correctAnswer);
156156
Log.d("TAG", "correct answer: " + correctAnswer);
157-
if (correctAnswer.size() == 0 || correctAnswer == null){
157+
if (correctAnswer.size() == 0 || correctAnswer == null) {
158158
allAnswers--;
159-
} else if (answer.equals(correctAnswer)){
159+
} else if (answer.equals(correctAnswer)) {
160160
correctAnswers++;
161161
}
162162
} catch (ClassCastException ignored) {
@@ -165,10 +165,10 @@ public float getPercentageOfCorrectAnswers(){
165165
}
166166
Log.d("TAG", "getPercentageOfCorrectAnswers: " + correctAnswers);
167167
Log.d("TAG", "getPercentageOfCorrectAnswers: " + allAnswers);
168-
return (float) correctAnswers/allAnswers;
168+
return (float) correctAnswers / allAnswers;
169169
}
170170

171-
public ArrayList<Object> getAnswers(){
171+
public ArrayList<Object> getAnswers() {
172172
ArrayList<Object> answers = new ArrayList<>();
173173
Log.d("answers", "list size: " + linearLayout.getChildCount());
174174
for (int i = 0; i < linearLayout.getChildCount(); i++) {
@@ -200,7 +200,7 @@ public ArrayList<Object> getAnswers(){
200200
return answers;
201201
}
202202

203-
public boolean areAllQuestionsAnswered(){
203+
public boolean areAllQuestionsAnswered() {
204204
Log.d("answers", "list size: " + linearLayout.getChildCount());
205205
for (int i = 0; i < linearLayout.getChildCount(); i++) {
206206
YesOrNoQuestion yesOrNoQuestion;
@@ -209,15 +209,15 @@ public boolean areAllQuestionsAnswered(){
209209

210210
try {
211211
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
212-
if (yesOrNoQuestion.getAnswer() == 0){
212+
if (yesOrNoQuestion.getAnswer() == 0) {
213213
return false;
214214
}
215215
} catch (ClassCastException ignored) {
216216

217217
}
218218
try {
219219
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
220-
if (multipleChoiceQuestion.getAnswer() == 0){
220+
if (multipleChoiceQuestion.getAnswer() == 0) {
221221
return false;
222222
}
223223
} catch (Exception ignored) {
@@ -226,7 +226,7 @@ public boolean areAllQuestionsAnswered(){
226226

227227
try {
228228
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
229-
if (multipleAnswerQuestion.getAnswer().size() == 0){
229+
if (multipleAnswerQuestion.getAnswer().size() == 0) {
230230
return false;
231231
}
232232
} catch (Exception ignored) {
@@ -236,7 +236,7 @@ public boolean areAllQuestionsAnswered(){
236236
return true;
237237
}
238238

239-
public View getQuestion(int index){
239+
public View getQuestion(int index) {
240240
YesOrNoQuestion yesOrNoQuestion = null;
241241
MultipleChoiceQuestion multipleChoiceQuestion = null;
242242
MultipleAnswerQuestion multipleAnswerQuestion = null;
@@ -259,27 +259,38 @@ public View getQuestion(int index){
259259

260260
}
261261

262-
if (yesOrNoQuestion != null){
262+
if (yesOrNoQuestion != null) {
263263
return yesOrNoQuestion;
264-
}else if (multipleChoiceQuestion != null){
264+
} else if (multipleChoiceQuestion != null) {
265265
return multipleChoiceQuestion;
266-
} else if(multipleAnswerQuestion != null){
266+
} else if (multipleAnswerQuestion != null) {
267267
return multipleAnswerQuestion;
268-
}else {
268+
} else {
269269
return null;
270270
}
271271
}
272272

273-
public void addOnValueChangedRunnable(int index, Runnable r){
273+
public void addOnAnswerChangedListener(int index, OnAnswerChangedListener r) {
274274
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+
279290
}
280291
}
281292

282-
public LinearLayout getQuestionViews(){
293+
public LinearLayout getQuestionViews() {
283294
return linearLayout;
284295
}
285296
}

CheckboxQuestion/CheckboxQuestion/src/main/java/com/aadyad/checkboxquestion/Views/MultipleAnswerQuestion.java

+28-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import androidx.annotation.Nullable;
1717

18+
import com.aadyad.checkboxquestion.OnAnswerChangedListener;
1819
import com.aadyad.checkboxquestion.Question;
1920
import com.aadyad.checkboxquestion.QuestionListSettings;
2021
import com.aadyad.checkboxquestion.R;
@@ -29,7 +30,7 @@ public class MultipleAnswerQuestion extends LinearLayout {
2930
public static final int RIGHT = 2;
3031
Context context;
3132

32-
private Runnable onValueChanged;
33+
private OnAnswerChangedListener onAnswerChangedListener;
3334

3435
ArrayList<CheckBox> checkBoxes = new ArrayList<>();
3536

@@ -41,11 +42,14 @@ public class MultipleAnswerQuestion extends LinearLayout {
4142
public MultipleAnswerQuestion(Context context) {
4243
this(context, null);
4344
this.context = context;
45+
this.onAnswerChangedListener = null;
4446
}
4547

4648
public MultipleAnswerQuestion(Context context, @Nullable AttributeSet attrs) {
4749
super(context, attrs);
4850
this.context = context;
51+
this.onAnswerChangedListener = null;
52+
4953
setOrientation(LinearLayout.VERTICAL);
5054
LayoutInflater.from(context).inflate(R.layout.multiple_answer_question, this, true);
5155

@@ -97,15 +101,6 @@ public void init(String title, String number, boolean numEnabled, int spacing, i
97101
layout = findViewById(R.id.multipleChoiceHolder);
98102
mainLayout = findViewById(R.id.mainLayout);
99103

100-
if (onValueChanged == null) {
101-
onValueChanged = new Runnable() {
102-
@Override
103-
public void run() {
104-
105-
}
106-
};
107-
}
108-
109104
this.spacing = spacing;
110105

111106
checkBoxes.add(option1);
@@ -176,7 +171,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
176171
//Remove selected answer to integer arraylist
177172
selectedAnswers.remove(selectedAnswer);
178173
}
179-
onValueChanged.run();
174+
if (onAnswerChangedListener != null) {
175+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
176+
}
180177
}
181178
});
182179

@@ -213,7 +210,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
213210
//Remove selected answer to integer arraylist
214211
selectedAnswers.remove(selectedAnswer);
215212
}
216-
onValueChanged.run();
213+
if (onAnswerChangedListener != null) {
214+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
215+
}
217216
}
218217
});
219218

@@ -244,7 +243,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
244243
//Remove selected answer to integer arraylist
245244
selectedAnswers.remove(selectedAnswer);
246245
}
247-
onValueChanged.run();
246+
if (onAnswerChangedListener != null) {
247+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
248+
}
248249
}
249250
});
250251

@@ -328,7 +329,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
328329
//Remove selected answer to integer arraylist
329330
selectedAnswers.remove(selectedAnswer);
330331
}
331-
onValueChanged.run();
332+
if (onAnswerChangedListener != null) {
333+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
334+
}
332335
}
333336
});
334337

@@ -343,7 +346,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
343346
//Remove selected answer to integer arraylist
344347
selectedAnswers.remove(selectedAnswer);
345348
}
346-
onValueChanged.run();
349+
if (onAnswerChangedListener != null) {
350+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
351+
}
347352
}
348353
});
349354

@@ -358,7 +363,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
358363
//Remove selected answer to integer arraylist
359364
selectedAnswers.remove(selectedAnswer);
360365
}
361-
onValueChanged.run();
366+
if (onAnswerChangedListener != null) {
367+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
368+
}
362369
}
363370
});
364371

@@ -373,7 +380,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
373380
//Remove selected answer to integer arraylist
374381
selectedAnswers.remove(selectedAnswer);
375382
}
376-
onValueChanged.run();
383+
if (onAnswerChangedListener != null) {
384+
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
385+
}
377386
}
378387
});
379388
}
@@ -474,8 +483,8 @@ public void setCheckboxOrientation(int orientation){
474483
}
475484
}
476485

477-
public void doOnValueChanged(Runnable runnable){
478-
this.onValueChanged = runnable;
486+
public void addOnAnswerChangedListener(OnAnswerChangedListener onAnswerChangedListener){
487+
this.onAnswerChangedListener = onAnswerChangedListener;
479488
}
480489

481490
public void setCheckedOption(int option){

0 commit comments

Comments
 (0)