I have a questionnaire form where the contents are dynamic (The questions and the spinner values can be updated from the site.). I need to be able to retrieve all questions from the REST api and show it dynamically in a TextView (This part is done) and show create user input answers in the form for Spinner or EditText based on the question. I am able to create spinners but not able to show the values in it.
List<QuestionBankListItem> result = response.body();
final TextView[] myTextViews = new TextView[result.size()];
for (int i = 0; i < result.size(); i++) {
QuestionBankListItem items = result.get(i);
final TextView rowTextView = new TextView(VisitorsSurveyActivity.this);
rowTextView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
rowTextView.setText(i + 1 + ". " + items.getsQuestion());
activityVisitorsSurveyBinding.rootLayout.addView(rowTextView);
myTextViews[i] = rowTextView;
switch (items.getiQuestionTypeID()) {
case 1:
final Spinner singleSelectSpinner = new Spinner(VisitorsSurveyActivity.this);
singleSelectSpinner.setAdapter(new ArrayAdapter<>(VisitorsSurveyActivity.this, android.R.layout.simple_list_item_1,
retrieveAnswerList(items.getId())));
activityVisitorsSurveyBinding.rootLayout.addView(singleSelectSpinner);
break;
case 2:
final Spinner multiSelectSpinner = new Spinner(VisitorsSurveyActivity.this);
break;
case 5:
final EditText freeText = new EditText(VisitorsSurveyActivity.this);
activityVisitorsSurveyBinding.rootLayout.addView(freeText);
break;
}
}
private List<AnswerBankListItem> retrieveAnswerList(int id) {
Call<List<AnswerBankListItem>> call = apiInterface.retrieveAnswerBankList("bearer " + sToken, id, 1);
call.enqueue(new Callback<List<AnswerBankListItem>>() {
@Override
public void onResponse(@NonNull Call<List<AnswerBankListItem>> call, @NonNull Response<List<AnswerBankListItem>> response) {
if (response.code() >= 400) {
Snackbar.make(activityVisitorsSurveyBinding.rootLayout, "Unauthorized Request", Snackbar.LENGTH_LONG).show();
} else if (response.code() >= 500) {
Snackbar.make(activityVisitorsSurveyBinding.rootLayout, "Server Error", Snackbar.LENGTH_LONG).show();
} else if (response.isSuccessful() && response.body() != null) {
answerResult = response.body();
}
}
@Override
public void onFailure(@NonNull Call<List<AnswerBankListItem>> call, @NonNull Throwable t) {
}
});
return answerResult;
}
What I have tried:
I'm able to retrieve the questions create textviews dynamically and show it in the textview. I'm also able to retrieve the answers in the form of List objects but not able to show the result in the spinner