Skip to content

Commit fa0851a

Browse files
committed
Added set answer methods
1 parent 4752a32 commit fa0851a

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void createQuestionViews() {
5959
case Question.MULTIPLE_ANSWER_QUESTION:
6060
MultipleAnswerQuestion multipleAnswerQuestion = new MultipleAnswerQuestion(context);
6161
multipleAnswerQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), q.options);
62+
multipleAnswerQuestion.setCorrectAnswer(q.multipleCorrectAnswer);
6263
linearLayout.addView(multipleAnswerQuestion);
6364
break;
6465
default:

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

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.Collections;
2526

2627
public class MultipleAnswerQuestion extends LinearLayout {
2728

@@ -40,6 +41,7 @@ public class MultipleAnswerQuestion extends LinearLayout {
4041
LinearLayout layout;
4142
LinearLayout mainLayout;
4243
private int spacing;
44+
private ArrayList<Integer> correctAnswer;
4345

4446
public MultipleAnswerQuestion(Context context) {
4547
this(context, null);
@@ -527,4 +529,21 @@ public TextView getQuestionNumberTextView(){
527529
public String[] getOptions(){
528530
return allOptions;
529531
}
532+
533+
public void setCorrectAnswer(ArrayList<Integer> correctAnswer){
534+
Collections.sort(correctAnswer);
535+
this.correctAnswer = correctAnswer;
536+
}
537+
538+
public ArrayList<Integer> getCorrectAnswer(){
539+
return correctAnswer;
540+
}
541+
542+
public boolean isAnswerCorrect() throws Exception{
543+
if (getCorrectAnswer() == null){
544+
throw new Exception("There is no correct answer for this question.");
545+
}
546+
Collections.sort(selectedAnswers);
547+
return getCorrectAnswer().equals(selectedAnswers);
548+
}
530549
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,8 @@ public boolean isAnswerCorrect() throws Exception{
544544
public String[] getOptions(){
545545
return allOptions;
546546
}
547+
548+
public void setCorrectAnswer(int correctAnswer){
549+
this.correctAnswer = correctAnswer;
550+
}
547551
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,8 @@ public boolean isAnswerCorrect() throws Exception{
239239
}
240240
return getCorrectAnswer() == getSelectedAnswer();
241241
}
242+
243+
public void setCorrectAnswer(int correctAnswer){
244+
this.correctAnswer = correctAnswer;
245+
}
242246
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +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;
1516
import com.aadyad.checkboxquestion.Views.MultipleChoiceQuestion;
1617

1718
import org.json.JSONArray;
@@ -123,7 +124,11 @@ public void onAnswerChanged(int selectedAnswerIndex, String selectedAnswerText)
123124

124125
@Override
125126
public void onAnswerChanged(ArrayList<Integer> listOfSelectedAnswerIndexes) {
126-
Toast.makeText(MainActivity.this, "Selected: " + listOfSelectedAnswerIndexes, Toast.LENGTH_SHORT).show();
127+
try {
128+
Toast.makeText(MainActivity.this, "" + ((MultipleAnswerQuestion) questionList.getQuestion(finalI1)).isAnswerCorrect(), Toast.LENGTH_SHORT).show();
129+
} catch (Exception e) {
130+
e.printStackTrace();
131+
}
127132
}
128133
});
129134
}

0 commit comments

Comments
 (0)