Skip to content

Commit

Permalink
Added multipleanswerquestions, multiple bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EunoiaC committed Apr 26, 2021
1 parent af2d499 commit cc3c072
Show file tree
Hide file tree
Showing 10 changed files with 807 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.util.Log;

import java.util.ArrayList;

public class Question {

//TODO: Create MultipleAnswerQuestions
Expand All @@ -22,6 +24,7 @@ public class Question {
String question;
String type;
int correctAnswer;
ArrayList<Integer> multipleCorrectAnswer;
String[] options = new String[]{"Yes", "No"};

public Question(String question, int correctAnswer, String type, String... options) {
Expand All @@ -33,6 +36,15 @@ public Question(String question, int correctAnswer, String type, String... optio
this.correctAnswer = correctAnswer;
}

public Question(String question, ArrayList<Integer> correctAnswer, String type, String... options) {
this.question = question;
this.type = type;
if (options != null){
this.options = options;
}
this.multipleCorrectAnswer = correctAnswer;
}

public Question(String question, String correctAnswer, String type, String... options) {
this.question = question;
this.type = type;
Expand All @@ -49,6 +61,7 @@ private int getIndexOfString(String s) {
return i;
}
}
Log.d("TAG", "answer not found: " + s);
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import android.view.View;
import android.widget.LinearLayout;

import com.aadyad.checkboxquestion.Views.MultipleAnswerQuestion;
import com.aadyad.checkboxquestion.Views.MultipleChoiceQuestion;
import com.aadyad.checkboxquestion.Views.YesOrNoQuestion;

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

public class QuestionList {

Expand Down Expand Up @@ -39,27 +44,33 @@ public void createQuestionViews(){
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (questions == null){
for (Question q : choiceQuestions){
if (q.type.equals(Question.MULTIPLE_CHOICE_QUESTION)) {
Log.d("TAG", "createQuestionViews: " + Arrays.toString(q.options));
MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion(context);
multipleChoiceQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), settings.getQuestionLayoutHeight(), settings.getOptionLayoutHeight(), q.options);
linearLayout.addView(multipleChoiceQuestion);
}else if (q.type.equals(Question.YES_OR_NO_QUESTION)){
YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion(context);
yesOrNoQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), settings.getQuestionLayoutHeight(), settings.getOptionLayoutHeight());
linearLayout.addView(yesOrNoQuestion);
} else if (q.type.equals(Question.MULTIPLE_ANSWER_QUESTION)){
//Todo: Add code for multipleanswerquestion
} else{
throw new RuntimeException("Type " + q.type + " does not exist.");
switch (q.type) {
case Question.MULTIPLE_CHOICE_QUESTION:
Log.d("TAG", "createQuestionViews: " + Arrays.toString(q.options));
MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion(context);
multipleChoiceQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), q.options);
linearLayout.addView(multipleChoiceQuestion);
break;
case Question.YES_OR_NO_QUESTION:
YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion(context);
yesOrNoQuestion.init(q.question, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize());
linearLayout.addView(yesOrNoQuestion);
break;
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);
linearLayout.addView(multipleAnswerQuestion);
break;
default:
throw new RuntimeException("Type " + q.type + " does not exist.");
}
i++;
}
} else {
for (String q : questions) {
i++;
YesOrNoQuestion yesOrNoQuestion = new YesOrNoQuestion(context);
yesOrNoQuestion.init(q, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize(), settings.getQuestionLayoutHeight(), settings.getOptionLayoutHeight());
yesOrNoQuestion.init(q, String.valueOf(i), settings.isNumEnabled(), settings.getSpacing(), this.orientation, settings.getCheckBoxLocation(), settings.getQuestionTextSize(), settings.getCheckBoxTextSize());
linearLayout.addView(yesOrNoQuestion);
}
}
Expand All @@ -69,19 +80,29 @@ public void createQuestionViews(){
public void setLayoutOrientation(int orientation){
this.orientation = orientation;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
YesOrNoQuestion yesOrNoQuestion = null;
MultipleChoiceQuestion multipleChoiceQuestion = null;
View v = linearLayout.getChildAt(i);
YesOrNoQuestion yesOrNoQuestion;
MultipleChoiceQuestion multipleChoiceQuestion;
MultipleAnswerQuestion multipleAnswerQuestion;

try {
yesOrNoQuestion = (YesOrNoQuestion) v;
} catch (ClassCastException e) {
multipleChoiceQuestion = (MultipleChoiceQuestion) v;
}
if (yesOrNoQuestion != null){
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
yesOrNoQuestion.setCheckboxOrientation(orientation);
}else {
assert multipleChoiceQuestion != null;
} catch (ClassCastException ignored) {

}

try {
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
multipleChoiceQuestion.setCheckboxOrientation(orientation);
} catch (ClassCastException ignored) {

}

try {
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
multipleAnswerQuestion.setCheckboxOrientation(orientation);
} catch (ClassCastException ignored) {

}
}
createQuestionViews();
Expand All @@ -91,15 +112,12 @@ public float getPercentageOfCorrectAnswers(){
int correctAnswers = 0;
int allAnswers = linearLayout.getChildCount();
for (int i = 0; i < linearLayout.getChildCount(); i++) {
YesOrNoQuestion yesOrNoQuestion = null;
MultipleChoiceQuestion multipleChoiceQuestion = null;
View v = linearLayout.getChildAt(i);
YesOrNoQuestion yesOrNoQuestion;
MultipleChoiceQuestion multipleChoiceQuestion;
MultipleAnswerQuestion multipleAnswerQuestion;

try {
yesOrNoQuestion = (YesOrNoQuestion) v;
} catch (ClassCastException e) {
multipleChoiceQuestion = (MultipleChoiceQuestion) v;
}
if (yesOrNoQuestion != null){
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
int answer = yesOrNoQuestion.getAnswer();
Log.d("TAG", "answer: " + answer);
int correctAnswer = choiceQuestions.get(i).correctAnswer;
Expand All @@ -109,7 +127,12 @@ public float getPercentageOfCorrectAnswers(){
} else if (answer == correctAnswer){
correctAnswers++;
}
}else {
} catch (ClassCastException ignored) {

}

try {
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
int answer = multipleChoiceQuestion.getAnswer();
Log.d("TAG", "answer: " + answer);
int correctAnswer = choiceQuestions.get(i).correctAnswer;
Expand All @@ -119,54 +142,95 @@ public float getPercentageOfCorrectAnswers(){
} else if (answer == correctAnswer){
correctAnswers++;
}
} catch (Exception ignored) {

}

try {
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
ArrayList<Integer> answer = multipleAnswerQuestion.getAnswer();
Collections.sort(answer);
Log.d("TAG", "answer: " + answer);
ArrayList<Integer> correctAnswer = choiceQuestions.get(i).multipleCorrectAnswer;
Collections.sort(correctAnswer);
Log.d("TAG", "correct answer: " + correctAnswer);
if (correctAnswer.size() == 0 || correctAnswer == null){
allAnswers--;
} else if (answer.equals(correctAnswer)){
correctAnswers++;
}
} catch (ClassCastException ignored) {

}
}
Log.d("TAG", "getPercentageOfCorrectAnswers: " + correctAnswers);
Log.d("TAG", "getPercentageOfCorrectAnswers: " + allAnswers);
return (float) correctAnswers/allAnswers;
}

public ArrayList<Integer> getAnswers(){
ArrayList<Integer> answers = new ArrayList<>();
public ArrayList<Object> getAnswers(){
ArrayList<Object> answers = new ArrayList<>();
Log.d("answers", "list size: " + linearLayout.getChildCount());
for (int i = 0; i < linearLayout.getChildCount(); i++) {
YesOrNoQuestion yesOrNoQuestion = null;
MultipleChoiceQuestion multipleChoiceQuestion = null;
View v = linearLayout.getChildAt(i);
YesOrNoQuestion yesOrNoQuestion;
MultipleChoiceQuestion multipleChoiceQuestion;
MultipleAnswerQuestion multipleAnswerQuestion;

try {
yesOrNoQuestion = (YesOrNoQuestion) v;
} catch (ClassCastException e) {
multipleChoiceQuestion = (MultipleChoiceQuestion) v;
}
if (yesOrNoQuestion != null){
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
answers.add(yesOrNoQuestion.getAnswer());
}else {
} catch (ClassCastException ignored) {

}

try {
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
answers.add(multipleChoiceQuestion.getAnswer());
} catch (Exception ignored) {

}

try {
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
answers.add(multipleAnswerQuestion.getAnswer());
} catch (Exception ignored) {

}
}
return answers;
}

public boolean areAllQuestionsAnswered(){
ArrayList<Integer> answers = new ArrayList<>();
Log.d("answers", "list size: " + linearLayout.getChildCount());
for (int i = 0; i < linearLayout.getChildCount(); i++) {
YesOrNoQuestion yesOrNoQuestion = null;
MultipleChoiceQuestion multipleChoiceQuestion = null;
View v = linearLayout.getChildAt(i);
YesOrNoQuestion yesOrNoQuestion;
MultipleChoiceQuestion multipleChoiceQuestion;
MultipleAnswerQuestion multipleAnswerQuestion;

try {
yesOrNoQuestion = (YesOrNoQuestion) v;
} catch (ClassCastException e) {
multipleChoiceQuestion = (MultipleChoiceQuestion) v;
}
if (yesOrNoQuestion != null){
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
if (yesOrNoQuestion.getAnswer() == 0){
return false;
}
}else {
} catch (ClassCastException ignored) {

}
try {
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
if (multipleChoiceQuestion.getAnswer() == 0){
return false;
}
} catch (Exception ignored) {

}

try {
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
if (multipleAnswerQuestion.getAnswer().size() == 0){
return false;
}
} catch (Exception ignored) {

}
}
return true;
Expand All @@ -175,24 +239,42 @@ public boolean areAllQuestionsAnswered(){
public View getQuestion(int index){
YesOrNoQuestion yesOrNoQuestion = null;
MultipleChoiceQuestion multipleChoiceQuestion = null;
MultipleAnswerQuestion multipleAnswerQuestion = null;
View v = linearLayout.getChildAt(index);
try {
yesOrNoQuestion = (YesOrNoQuestion) v;
} catch (ClassCastException e) {
} catch (ClassCastException ignored) {

}

try {
multipleChoiceQuestion = (MultipleChoiceQuestion) v;
} catch (Exception ignored) {

}

try {
multipleAnswerQuestion = (MultipleAnswerQuestion) v;
} catch (Exception ignored) {

}

if (yesOrNoQuestion != null){
return yesOrNoQuestion;
}else {
}else if (multipleChoiceQuestion != null){
return multipleChoiceQuestion;
} else if(multipleAnswerQuestion != null){
return multipleAnswerQuestion;
}else {
return null;
}
}

public void addOnValueChangedRunnable(int index, Runnable r){
try {
((MultipleChoiceQuestion) getQuestion(index)).doOnValueChanged(r);
} catch (Exception e) {
YesOrNoQuestion yesOrNoQuestion = (YesOrNoQuestion) linearLayout.getChildAt(index);
YesOrNoQuestion yesOrNoQuestion = (YesOrNoQuestion) getQuestion(index);
//Todo: add do on value changed
}
}
Expand Down
Loading

0 comments on commit cc3c072

Please sign in to comment.