Skip to content

Commit 9f29de9

Browse files
committed
Renamed and added methods
1 parent 1f7c798 commit 9f29de9

File tree

6 files changed

+100
-28
lines changed

6 files changed

+100
-28
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public void createQuestionViews() {
4848
case Question.MULTIPLE_CHOICE_QUESTION:
4949
Log.d("TAG", "createQuestionViews: " + Arrays.toString(q.options));
5050
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);
51+
multipleChoiceQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), q.correctAnswer, q.options);
5252
linearLayout.addView(multipleChoiceQuestion);
5353
break;
5454
case Question.YES_OR_NO_QUESTION:
5555
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());
56+
yesOrNoQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), q.correctAnswer);
5757
linearLayout.addView(yesOrNoQuestion);
5858
break;
5959
case Question.MULTIPLE_ANSWER_QUESTION:
@@ -70,7 +70,7 @@ public void createQuestionViews() {
7070
for (String q : questions) {
7171
i++;
7272
YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion(context);
73-
yesOrNoQuestion.init(q, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize());
73+
yesOrNoQuestion.init(q, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), Question.NO_ANSWER);
7474
linearLayout.addView(yesOrNoQuestion);
7575
}
7676
}
@@ -118,11 +118,11 @@ public float getPercentageOfCorrectAnswers() {
118118

119119
try {
120120
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
121-
int answer = yesOrNoQuestion.getAnswer();
121+
int answer = yesOrNoQuestion.getSelectedAnswer();
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 == Question.NO_ANSWER) {
126126
allAnswers--;
127127
} else if (answer == correctAnswer) {
128128
correctAnswers++;
@@ -133,11 +133,11 @@ public float getPercentageOfCorrectAnswers() {
133133

134134
try {
135135
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
136-
int answer = multipleChoiceQuestion.getAnswer();
136+
int answer = multipleChoiceQuestion.getSelectedAnswer();
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 == Question.NO_ANSWER) {
141141
allAnswers--;
142142
} else if (answer == correctAnswer) {
143143
correctAnswers++;
@@ -148,13 +148,13 @@ public float getPercentageOfCorrectAnswers() {
148148

149149
try {
150150
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
151-
ArrayList<Integer> answer = multipleAnswerQuestion.getAnswer();
151+
ArrayList<Integer> answer = multipleAnswerQuestion.getSelectedAnswers();
152152
Collections.sort(answer);
153153
Log.d("TAG", "answer: " + answer);
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() == Question.NO_ANSWER || correctAnswer == null) {
158158
allAnswers--;
159159
} else if (answer.equals(correctAnswer)) {
160160
correctAnswers++;
@@ -168,7 +168,7 @@ public float getPercentageOfCorrectAnswers() {
168168
return (float) correctAnswers / allAnswers;
169169
}
170170

171-
public ArrayList<Object> getAnswers() {
171+
public ArrayList<Object> getSelectedAnswers() {
172172
ArrayList<Object> answers = new ArrayList<>();
173173
Log.d("answers", "list size: " + linearLayout.getChildCount());
174174
for (int i = 0; i < linearLayout.getChildCount(); i++) {
@@ -178,21 +178,21 @@ public ArrayList<Object> getAnswers() {
178178

179179
try {
180180
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
181-
answers.add(yesOrNoQuestion.getAnswer());
181+
answers.add(yesOrNoQuestion.getSelectedAnswer());
182182
} catch (ClassCastException ignored) {
183183

184184
}
185185

186186
try {
187187
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
188-
answers.add(multipleChoiceQuestion.getAnswer());
188+
answers.add(multipleChoiceQuestion.getSelectedAnswer());
189189
} catch (Exception ignored) {
190190

191191
}
192192

193193
try {
194194
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
195-
answers.add(multipleAnswerQuestion.getAnswer());
195+
answers.add(multipleAnswerQuestion.getSelectedAnswers());
196196
} catch (Exception ignored) {
197197

198198
}
@@ -209,15 +209,15 @@ public boolean areAllQuestionsAnswered() {
209209

210210
try {
211211
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
212-
if (yesOrNoQuestion.getAnswer() == 0) {
212+
if (yesOrNoQuestion.getSelectedAnswer() == 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.getSelectedAnswer() == 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.getSelectedAnswers().size() == 0) {
230230
return false;
231231
}
232232
} catch (Exception ignored) {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class MultipleAnswerQuestion extends LinearLayout {
3535
private OnAnswerChangedListener onAnswerChangedListener;
3636

3737
ArrayList<CheckBox> checkBoxes = new ArrayList<>();
38-
3938
ArrayList<Integer> selectedAnswers;
39+
4040
LinearLayout layout;
4141
LinearLayout mainLayout;
4242
private int spacing;
@@ -390,7 +390,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
390390
});
391391
}
392392

393-
public ArrayList<Integer> getAnswer() {
393+
public ArrayList<Integer> getSelectedAnswers() {
394394
return selectedAnswers;
395395
}
396396

@@ -512,6 +512,18 @@ public void setCheckedOption(String option){
512512
}
513513
}
514514

515+
public CheckBox getCheckbox(int index){
516+
return checkBoxes.get(index);
517+
}
518+
519+
public TextView getQuestionTitleTextView(){
520+
return findViewById(R.id.question_title);
521+
}
522+
523+
public TextView getQuestionNumberTextView(){
524+
return findViewById(R.id.question_number);
525+
}
526+
515527
public String[] getOptions(){
516528
return allOptions;
517529
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class MultipleChoiceQuestion extends LinearLayout {
3838
LinearLayout layout;
3939
LinearLayout mainLayout;
4040
private int spacing;
41+
private int correctAnswer;
4142

4243
public MultipleChoiceQuestion(Context context) {
4344
this(context, null);
@@ -59,6 +60,7 @@ public MultipleChoiceQuestion(Context context, @Nullable AttributeSet attrs) {
5960
String a3;
6061
String a4;
6162
int spacing;
63+
int tempCorrectAnswer;
6264
float questionTextSize, optionTextSize;
6365
int boxLocation;
6466
int orientation;
@@ -74,6 +76,7 @@ public MultipleChoiceQuestion(Context context, @Nullable AttributeSet attrs) {
7476
a2 = a.getString(R.styleable.MultipleChoiceQuestion_option_2);
7577
a3 = a.getString(R.styleable.MultipleChoiceQuestion_option_3);
7678
a4 = a.getString(R.styleable.MultipleChoiceQuestion_option_4);
79+
tempCorrectAnswer = a.getInt(R.styleable.MultipleChoiceQuestion_correct_answer, 0);
7780
title = a.getString(R.styleable.MultipleChoiceQuestion_question_title);
7881
boxLocation = a.getInt(R.styleable.MultipleChoiceQuestion_checkbox_location, 0);
7982
orientation = a.getInt(R.styleable.MultipleChoiceQuestion_checkbox_orientation, 0);
@@ -84,11 +87,11 @@ public MultipleChoiceQuestion(Context context, @Nullable AttributeSet attrs) {
8487
a.recycle();
8588
}
8689

87-
init(title, number, numberEnabled, spacing, orientation, boxLocation, questionTextSize, optionTextSize, a1, a2, a3, a4);
90+
init(title, number, numberEnabled, spacing, orientation, boxLocation, questionTextSize, optionTextSize, tempCorrectAnswer, a1, a2, a3, a4);
8891
}
8992

9093
// Setup views
91-
public void init(String title, String number, boolean numEnabled, int spacing, int orientation, int boxLocation, float questionTextSize, float optionTextSize, String... options) {
94+
public void init(String title, String number, boolean numEnabled, int spacing, int orientation, int boxLocation, float questionTextSize, float optionTextSize, int correctAnswer, String... options) {
9295
TextView questionTitle = (TextView) findViewById(R.id.question_title);
9396
TextView questionNumber = (TextView) findViewById(R.id.question_number);
9497
final CheckBox option1 = (CheckBox) findViewById(R.id.answer1);
@@ -100,6 +103,7 @@ public void init(String title, String number, boolean numEnabled, int spacing, i
100103

101104
this.spacing = spacing;
102105
this.allOptions = options;
106+
this.correctAnswer = correctAnswer;
103107

104108
checkBoxes.add(option1);
105109
checkBoxes.add(option2);
@@ -390,7 +394,7 @@ public void onClick(View v) {
390394
});
391395
}
392396

393-
public int getAnswer() {
397+
public int getSelectedAnswer() {
394398
return buttonClicked;
395399
}
396400

@@ -514,6 +518,29 @@ public void setCheckedOption(String option) {
514518
}
515519
}
516520

521+
public CheckBox getCheckbox(int index){
522+
return checkBoxes.get(index);
523+
}
524+
525+
public TextView getQuestionTitleTextView(){
526+
return findViewById(R.id.question_title);
527+
}
528+
529+
public TextView getQuestionNumberTextView(){
530+
return findViewById(R.id.question_number);
531+
}
532+
533+
public int getCorrectAnswer(){
534+
return correctAnswer;
535+
}
536+
537+
public boolean isAnswerCorrect() throws Exception{
538+
if (getCorrectAnswer() == Question.NO_ANSWER){
539+
throw new Exception("There is no correct answer for this question.");
540+
}
541+
return getCorrectAnswer() == getSelectedAnswer();
542+
}
543+
517544
public String[] getOptions(){
518545
return allOptions;
519546
}

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class YesOrNoQuestion extends LinearLayout {
2929

3030
private LinearLayout layout;
3131
private int spacing;
32+
private int correctAnswer;
3233

3334
private int buttonClicked = 0; //0 is not clicked, 1 is no, 2 is yes
3435

@@ -47,6 +48,7 @@ public YesOrNoQuestion(Context context, @Nullable AttributeSet attrs) {
4748
String number;
4849
int spacing;
4950
int boxLocation;
51+
int tempCorrectAnswer;
5052
int orientation;
5153
float questionTextSize;
5254
float checkBoxTextSize;
@@ -59,6 +61,7 @@ public YesOrNoQuestion(Context context, @Nullable AttributeSet attrs) {
5961
checkBoxTextSize = a.getFloat(R.styleable.YesOrNoQuestion_option_text_size, QuestionListSettings.TEXT_SIZE_DEFAULT);
6062
//questionLayoutHeight = a.getFloat(R.styleable.YesOrNoQuestion_question_layout_height, QuestionListSettings.TEXT_SIZE_DEFAULT);
6163
title = a.getString(R.styleable.YesOrNoQuestion_question_title);
64+
tempCorrectAnswer = a.getInt(R.styleable.YesOrNoQuestion_correct_answer, 0);
6265
boxLocation = a.getInt(R.styleable.YesOrNoQuestion_checkbox_location, 0);
6366
orientation = a.getInt(R.styleable.YesOrNoQuestion_checkbox_orientation, 0);
6467
spacing = a.getInt(R.styleable.YesOrNoQuestion_spacing_between_boxes, 17);
@@ -68,15 +71,16 @@ public YesOrNoQuestion(Context context, @Nullable AttributeSet attrs) {
6871
a.recycle();
6972
}
7073

71-
init(title, number, numberEnabled, spacing, orientation, boxLocation, questionTextSize, checkBoxTextSize);
74+
init(title, number, numberEnabled, spacing, orientation, boxLocation, questionTextSize, checkBoxTextSize, correctAnswer);
7275
}
7376

7477
// Setup views
75-
public void init(String title, String number, boolean numEnabled, int spacing, int orientation, int boxLocation, float questionSize, float checkBoxSize) {
78+
public void init(String title, String number, boolean numEnabled, int spacing, int orientation, int boxLocation, float questionSize, float checkBoxSize, int correctAnswer) {
7679
TextView questionNumber = (TextView) findViewById(R.id.question_number);
7780
layout = findViewById(R.id.checkBoxHolder);
7881

7982
this.spacing = spacing;
83+
this.correctAnswer = correctAnswer;
8084

8185
//Todo: Set height if size == auto.
8286
//Todo: Also use height
@@ -132,7 +136,7 @@ public void onClick(View v) {
132136
});
133137
}
134138

135-
public int getAnswer(){
139+
public int getSelectedAnswer(){
136140
return buttonClicked;
137141
}
138142

@@ -216,4 +220,23 @@ public void setCheckedOption(String option){
216220
no.setChecked(true);
217221
}
218222
}
223+
224+
public TextView getQuestionTitleTextView(){
225+
return findViewById(R.id.question_title);
226+
}
227+
228+
public TextView getQuestionNumberTextView(){
229+
return findViewById(R.id.question_number);
230+
}
231+
232+
public int getCorrectAnswer(){
233+
return correctAnswer;
234+
}
235+
236+
public boolean isAnswerCorrect() throws Exception{
237+
if (getCorrectAnswer() == Question.NO_ANSWER){
238+
throw new Exception("There is no correct answer for this question.");
239+
}
240+
return getCorrectAnswer() == getSelectedAnswer();
241+
}
219242
}

CheckboxQuestion/CheckboxQuestion/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<declare-styleable name="YesOrNoQuestion">
44
<attr name="option_text_size" format="float"/>
55
<attr name="question_text_size" format="float"/>
6+
<attr name="correct_answer" format="integer"/>
67
<attr name="checkbox_orientation" format="integer">
78
<enum name="horizontal" value="0"/>
89
<enum name="split_vertical" value="1"/>
@@ -22,6 +23,7 @@
2223
</declare-styleable>
2324

2425
<declare-styleable name="MultipleChoiceQuestion">
26+
<attr name="correct_answer"/>
2527
<attr name="option_text_size"/>
2628
<attr name="question_text_size"/>
2729
<attr name="checkbox_orientation"/>

CheckboxQuestion/app/src/main/java/com/aadyad/checkboxquestions_library/MainActivity.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import com.aadyad.checkboxquestion.Question;
1313
import com.aadyad.checkboxquestion.QuestionList;
1414
import com.aadyad.checkboxquestion.QuestionListSettings;
15-
import com.aadyad.checkboxquestion.Views.MultipleAnswerQuestion;
1615
import com.aadyad.checkboxquestion.Views.MultipleChoiceQuestion;
17-
import com.aadyad.checkboxquestion.Views.YesOrNoQuestion;
1816

1917
import org.json.JSONArray;
2018
import org.json.JSONException;
@@ -111,10 +109,16 @@ public void run() {
111109
questionList.createQuestionViews();
112110

113111
for (int i = 0; i < questionList.getQuestionViews().getChildCount(); i++){
112+
final int finalI = i;
113+
final int finalI1 = i;
114114
questionList.addOnAnswerChangedListener(i, new OnAnswerChangedListener() {
115115
@Override
116116
public void onAnswerChanged(int selectedAnswerIndex, String selectedAnswerText) {
117-
Toast.makeText(MainActivity.this, "Selected: " + selectedAnswerText, Toast.LENGTH_SHORT).show();
117+
try {
118+
Toast.makeText(MainActivity.this, "" + ((MultipleChoiceQuestion) questionList.getQuestion(finalI1)).isAnswerCorrect(), Toast.LENGTH_SHORT).show();
119+
} catch (Exception e) {
120+
e.printStackTrace();
121+
}
118122
}
119123

120124
@Override
@@ -139,11 +143,15 @@ public void logAnswers(View v) {
139143
Log.d("Questions answered", "logAnswers: " + questionList.areAllQuestionsAnswered());
140144
Log.d("Questions answered", "percentage: " + questionList.getPercentageOfCorrectAnswers());
141145
String s = "";
142-
for (Object i : questionList.getAnswers()) {
146+
for (Object i : questionList.getSelectedAnswers()) {
143147
try {
144148
s += (int) i;
145149
} catch (Exception ignored) {
146150
}
151+
try {
152+
s += (ArrayList<Integer>) i;
153+
} catch (Exception ignored) {
154+
}
147155
}
148156

149157
Log.d("answers", "logAnswers: " + s);

0 commit comments

Comments
 (0)