Challenge: Preventing Repeat Answers: How to best save which question has been answered

I believe this was a very smart way to do things, the Getter and Setter within the Question class is brilliant.

I put them in a HashMap and when clicked, I added the true value to the map, then when the user answers the question the button are disabled/enabled accordingly.

private HashMap<Question, Boolean> mAnsweredQuestions = new HashMap<>();

mAnsweredQuestions.put(mQuestionBank[mCurrentIndex], true);

if (mAnsweredQuestions.containsKey(mQuestionBank[mCurrentIndex])) {
                mTrueButton.setEnabled(false);
                mFalseButton.setEnabled(false);
            } else {
                mTrueButton.setEnabled(true);
                mFalseButton.setEnabled(true);
            }

I am having issues with the screen rotating though. It seems the addresses that it points to in a memory are different so having the serializable isn’t the way I should do it.

mAnsweredQuestions = (HashMap<Question, Boolean>) savedInstanceState.getSerializable("mAnsweredQuestionsHashMap");

outState.putSerializable("mAnsweredQuestionsHashMap", mAnsweredQuestions);

I’ve found a solution using pieces from your solution.

1 Like