use of com.instructure.candroid.adapter.QuizMultipleDropdownSpinnerAdapter in project instructure-android by instructure.
the class QuizMultipleDropdownBinder method bind.
public static void bind(final QuizMultipleDropdownViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int courseColor, final int position, final boolean shouldLetAnswer, final Context context, final CanvasWebView.CanvasEmbeddedWebViewCallback embeddedWebViewCallback, final CanvasWebView.CanvasWebViewClientCallback webViewClientCallback, final QuizPostMultipleDropdown callback, final QuizToggleFlagState flagStateCallback) {
if (holder == null) {
return;
}
setupViews(holder, quizSubmissionQuestion, position, context, embeddedWebViewCallback, webViewClientCallback);
LayoutInflater inflater = LayoutInflater.from(context);
// use a map to store the data, each blank id will have an array list of potential answers
HashMap<String, ArrayList<QuizSubmissionAnswer>> answerMap = new HashMap<>();
for (QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
ArrayList<QuizSubmissionAnswer> potentialAnswers = answerMap.get(answer.getBlankId());
if (potentialAnswers == null) {
potentialAnswers = new ArrayList<>();
QuizSubmissionAnswer selectAnswer = new QuizSubmissionAnswer();
selectAnswer.setText(context.getString(R.string.quizMatchingDefaultDisplay));
potentialAnswers.add(selectAnswer);
}
potentialAnswers.add(answer);
answerMap.put(answer.getBlankId(), potentialAnswers);
}
if (answerMap.size() > 1) {
// we have more than one, update the string to reflect that
holder.chooseAnswer.setText(context.getString(R.string.choose_answers_below));
} else {
holder.chooseAnswer.setText(context.getString(R.string.choose_answer_below));
}
// add answers to the answer container
for (final String blankId : answerMap.keySet()) {
final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_multiple_dropdown_answer, null, false);
final TextView answerTextView = (TextView) answerWrapper.findViewById(R.id.text_answer);
final Spinner spinner = (Spinner) answerWrapper.findViewById(R.id.answer_spinner);
ArrayList<QuizSubmissionAnswer> list = new ArrayList<>();
for (QuizSubmissionAnswer dropdownAnswer : answerMap.get(blankId)) {
list.add(dropdownAnswer);
}
QuizMultipleDropdownSpinnerAdapter adapter = new QuizMultipleDropdownSpinnerAdapter(context, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if (!TextUtils.isEmpty(blankId)) {
answerTextView.setText(blankId);
setPreviouslySelectedAnswer(quizSubmissionQuestion, blankId, spinner, list);
}
final Drawable courseColorFlag = ColorKeeper.getColoredDrawable(context, R.drawable.vd_bookmark_filled, courseColor);
if (quizSubmissionQuestion.isFlagged()) {
holder.flag.setImageDrawable(courseColorFlag);
} else {
holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
}
if (shouldLetAnswer) {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// post the answer to the api
HashMap<String, Long> answerMap = new HashMap<>();
if (((QuizSubmissionAnswer) adapterView.getSelectedItem()).getId() != 0) {
answerMap.put(blankId, ((QuizSubmissionAnswer) adapterView.getSelectedItem()).getId());
callback.postMultipleDropdown(holder.questionId, answerMap);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
holder.flag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quizSubmissionQuestion.isFlagged()) {
// unflag it
holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
flagStateCallback.toggleFlagged(false, quizSubmissionQuestion.getId());
quizSubmissionQuestion.setFlagged(false);
} else {
// flag it
holder.flag.setImageDrawable(courseColorFlag);
flagStateCallback.toggleFlagged(true, quizSubmissionQuestion.getId());
quizSubmissionQuestion.setFlagged(true);
}
}
});
} else {
holder.flag.setEnabled(false);
}
holder.answerContainer.addView(answerWrapper);
}
}
Aggregations