Skip to content

Commit

Permalink
Added set answer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
EunoiaC committed May 6, 2021
1 parent 4752a32 commit fa0851a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void createQuestionViews() {
case Question.MULTIPLE_ANSWER_QUESTION:
MultipleAnswerQuestion multipleAnswerQuestion = new MultipleAnswerQuestion(context);
multipleAnswerQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), q.options);
multipleAnswerQuestion.setCorrectAnswer(q.multipleCorrectAnswer);
linearLayout.addView(multipleAnswerQuestion);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class MultipleAnswerQuestion extends LinearLayout {

Expand All @@ -40,6 +41,7 @@ public class MultipleAnswerQuestion extends LinearLayout {
LinearLayout layout;
LinearLayout mainLayout;
private int spacing;
private ArrayList<Integer> correctAnswer;

public MultipleAnswerQuestion(Context context) {
this(context, null);
Expand Down Expand Up @@ -527,4 +529,21 @@ public TextView getQuestionNumberTextView(){
public String[] getOptions(){
return allOptions;
}

public void setCorrectAnswer(ArrayList<Integer> correctAnswer){
Collections.sort(correctAnswer);
this.correctAnswer = correctAnswer;
}

public ArrayList<Integer> getCorrectAnswer(){
return correctAnswer;
}

public boolean isAnswerCorrect() throws Exception{
if (getCorrectAnswer() == null){
throw new Exception("There is no correct answer for this question.");
}
Collections.sort(selectedAnswers);
return getCorrectAnswer().equals(selectedAnswers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,8 @@ public boolean isAnswerCorrect() throws Exception{
public String[] getOptions(){
return allOptions;
}

public void setCorrectAnswer(int correctAnswer){
this.correctAnswer = correctAnswer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,8 @@ public boolean isAnswerCorrect() throws Exception{
}
return getCorrectAnswer() == getSelectedAnswer();
}

public void setCorrectAnswer(int correctAnswer){
this.correctAnswer = correctAnswer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.aadyad.checkboxquestion.Question;
import com.aadyad.checkboxquestion.QuestionList;
import com.aadyad.checkboxquestion.QuestionListSettings;
import com.aadyad.checkboxquestion.Views.MultipleAnswerQuestion;
import com.aadyad.checkboxquestion.Views.MultipleChoiceQuestion;

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

@Override
public void onAnswerChanged(ArrayList<Integer> listOfSelectedAnswerIndexes) {
Toast.makeText(MainActivity.this, "Selected: " + listOfSelectedAnswerIndexes, Toast.LENGTH_SHORT).show();
try {
Toast.makeText(MainActivity.this, "" + ((MultipleAnswerQuestion) questionList.getQuestion(finalI1)).isAnswerCorrect(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down

0 comments on commit fa0851a

Please sign in to comment.