Skip to content

Commit

Permalink
Renamed methods and added OnAnswerChangedListener, added OnAnswerChan…
Browse files Browse the repository at this point in the history
…gedListener for yesornoquestion as well.
  • Loading branch information
EunoiaC committed Apr 29, 2021
1 parent 57d22cb commit 63d964b
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.aadyad.checkboxquestion;

import java.util.ArrayList;

public interface OnAnswerChangedListener {
public void onAnswerChanged(int answer);
public void onAnswerChanged(ArrayList<Integer> answer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ public class QuestionList {
Context context;
int orientation;

public QuestionList(ArrayList<Question> questions, QuestionListSettings settings, Context context){
public QuestionList(ArrayList<Question> questions, QuestionListSettings settings, Context context) {
choiceQuestions = questions;
this.settings = settings;
this.context = context;
this.orientation = settings.getCheckBoxOrientation();
linearLayout = new LinearLayout(context);
}

public QuestionList(String[] questions, QuestionListSettings settings, Context context){
public QuestionList(String[] questions, QuestionListSettings settings, Context context) {
this.questions = questions;
this.settings = settings;
this.orientation = settings.getCheckBoxOrientation();
this.context = context;
linearLayout = new LinearLayout(context);
}

public void createQuestionViews(){
public void createQuestionViews() {
int i = 1;
linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (questions == null){
for (Question q : choiceQuestions){
if (questions == null) {
for (Question q : choiceQuestions) {
switch (q.type) {
case Question.MULTIPLE_CHOICE_QUESTION:
Log.d("TAG", "createQuestionViews: " + Arrays.toString(q.options));
Expand Down Expand Up @@ -77,7 +77,7 @@ public void createQuestionViews(){

}

public void setLayoutOrientation(int orientation){
public void setLayoutOrientation(int orientation) {
this.orientation = orientation;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
YesOrNoQuestion yesOrNoQuestion;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void setLayoutOrientation(int orientation){
createQuestionViews();
}

public float getPercentageOfCorrectAnswers(){
public float getPercentageOfCorrectAnswers() {
int correctAnswers = 0;
int allAnswers = linearLayout.getChildCount();
for (int i = 0; i < linearLayout.getChildCount(); i++) {
Expand All @@ -122,9 +122,9 @@ public float getPercentageOfCorrectAnswers(){
Log.d("TAG", "answer: " + answer);
int correctAnswer = choiceQuestions.get(i).correctAnswer;
Log.d("TAG", "correct answer: " + correctAnswer);
if (correctAnswer == 0){
if (correctAnswer == 0) {
allAnswers--;
} else if (answer == correctAnswer){
} else if (answer == correctAnswer) {
correctAnswers++;
}
} catch (ClassCastException ignored) {
Expand All @@ -137,9 +137,9 @@ public float getPercentageOfCorrectAnswers(){
Log.d("TAG", "answer: " + answer);
int correctAnswer = choiceQuestions.get(i).correctAnswer;
Log.d("TAG", "correct answer: " + correctAnswer);
if (correctAnswer == 0){
if (correctAnswer == 0) {
allAnswers--;
} else if (answer == correctAnswer){
} else if (answer == correctAnswer) {
correctAnswers++;
}
} catch (Exception ignored) {
Expand All @@ -154,9 +154,9 @@ public float getPercentageOfCorrectAnswers(){
ArrayList<Integer> correctAnswer = choiceQuestions.get(i).multipleCorrectAnswer;
Collections.sort(correctAnswer);
Log.d("TAG", "correct answer: " + correctAnswer);
if (correctAnswer.size() == 0 || correctAnswer == null){
if (correctAnswer.size() == 0 || correctAnswer == null) {
allAnswers--;
} else if (answer.equals(correctAnswer)){
} else if (answer.equals(correctAnswer)) {
correctAnswers++;
}
} catch (ClassCastException ignored) {
Expand All @@ -165,10 +165,10 @@ public float getPercentageOfCorrectAnswers(){
}
Log.d("TAG", "getPercentageOfCorrectAnswers: " + correctAnswers);
Log.d("TAG", "getPercentageOfCorrectAnswers: " + allAnswers);
return (float) correctAnswers/allAnswers;
return (float) correctAnswers / allAnswers;
}

public ArrayList<Object> getAnswers(){
public ArrayList<Object> getAnswers() {
ArrayList<Object> answers = new ArrayList<>();
Log.d("answers", "list size: " + linearLayout.getChildCount());
for (int i = 0; i < linearLayout.getChildCount(); i++) {
Expand Down Expand Up @@ -200,7 +200,7 @@ public ArrayList<Object> getAnswers(){
return answers;
}

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

try {
yesOrNoQuestion = (YesOrNoQuestion) getQuestion(i);
if (yesOrNoQuestion.getAnswer() == 0){
if (yesOrNoQuestion.getAnswer() == 0) {
return false;
}
} catch (ClassCastException ignored) {

}
try {
multipleChoiceQuestion = (MultipleChoiceQuestion) getQuestion(i);
if (multipleChoiceQuestion.getAnswer() == 0){
if (multipleChoiceQuestion.getAnswer() == 0) {
return false;
}
} catch (Exception ignored) {
Expand All @@ -226,7 +226,7 @@ public boolean areAllQuestionsAnswered(){

try {
multipleAnswerQuestion = (MultipleAnswerQuestion) getQuestion(i);
if (multipleAnswerQuestion.getAnswer().size() == 0){
if (multipleAnswerQuestion.getAnswer().size() == 0) {
return false;
}
} catch (Exception ignored) {
Expand All @@ -236,7 +236,7 @@ public boolean areAllQuestionsAnswered(){
return true;
}

public View getQuestion(int index){
public View getQuestion(int index) {
YesOrNoQuestion yesOrNoQuestion = null;
MultipleChoiceQuestion multipleChoiceQuestion = null;
MultipleAnswerQuestion multipleAnswerQuestion = null;
Expand All @@ -259,27 +259,38 @@ public View getQuestion(int index){

}

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

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

}

try {
((MultipleAnswerQuestion) getQuestion(index)).addOnAnswerChangedListener(r);
} catch (Exception ignored) {

}

try {
((YesOrNoQuestion) getQuestion(index)).addOnAnswerChangedListener(r);
} catch (Exception ignored) {

}
}

public LinearLayout getQuestionViews(){
public LinearLayout getQuestionViews() {
return linearLayout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import androidx.annotation.Nullable;

import com.aadyad.checkboxquestion.OnAnswerChangedListener;
import com.aadyad.checkboxquestion.Question;
import com.aadyad.checkboxquestion.QuestionListSettings;
import com.aadyad.checkboxquestion.R;
Expand All @@ -29,7 +30,7 @@ public class MultipleAnswerQuestion extends LinearLayout {
public static final int RIGHT = 2;
Context context;

private Runnable onValueChanged;
private OnAnswerChangedListener onAnswerChangedListener;

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

Expand All @@ -41,11 +42,14 @@ public class MultipleAnswerQuestion extends LinearLayout {
public MultipleAnswerQuestion(Context context) {
this(context, null);
this.context = context;
this.onAnswerChangedListener = null;
}

public MultipleAnswerQuestion(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.onAnswerChangedListener = null;

setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(context).inflate(R.layout.multiple_answer_question, this, true);

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

if (onValueChanged == null) {
onValueChanged = new Runnable() {
@Override
public void run() {

}
};
}

this.spacing = spacing;

checkBoxes.add(option1);
Expand Down Expand Up @@ -176,7 +171,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});

Expand Down Expand Up @@ -213,7 +210,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});

Expand Down Expand Up @@ -244,7 +243,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});

Expand Down Expand Up @@ -328,7 +329,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});

Expand All @@ -343,7 +346,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});

Expand All @@ -358,7 +363,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});

Expand All @@ -373,7 +380,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Remove selected answer to integer arraylist
selectedAnswers.remove(selectedAnswer);
}
onValueChanged.run();
if (onAnswerChangedListener != null) {
onAnswerChangedListener.onAnswerChanged(selectedAnswers);
}
}
});
}
Expand Down Expand Up @@ -474,8 +483,8 @@ public void setCheckboxOrientation(int orientation){
}
}

public void doOnValueChanged(Runnable runnable){
this.onValueChanged = runnable;
public void addOnAnswerChangedListener(OnAnswerChangedListener onAnswerChangedListener){
this.onAnswerChangedListener = onAnswerChangedListener;
}

public void setCheckedOption(int option){
Expand Down
Loading

0 comments on commit 63d964b

Please sign in to comment.