Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
// Question List result
List<QuestionBankListItem> result = response.body();
//Create TextView
          final TextView[] myTextViews = new TextView[result.size()];
          for (int i = 0; i < result.size(); i++) {
            QuestionBankListItem items = result.get(i);
            // Create Labels dynamically
            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;

            //answerResult = retrieveAnswerList(items.getId());

            switch (items.getiQuestionTypeID()) {
              case 1: // Single Select Spinner
                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: // Multi selectSpinner
                final Spinner multiSelectSpinner = new Spinner(VisitorsSurveyActivity.this);
                break;

              case 5: // Edit Text
                final EditText freeText = new EditText(VisitorsSurveyActivity.this);
                activityVisitorsSurveyBinding.rootLayout.addView(freeText);
                break;
            }

          } 


Java
// Method to retrieve Answers based on question
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
Posted
Comments
David Crow 8-May-20 15:20pm    
I assume you are referring to case 1. If so, could singleSelectSpinner be going out of scope?

Is it your intent to have the switch()/case statements inside the for() loop?
Rahul Ramakrishnan 9-May-20 2:51am    
The REST api that gets the questions has parameter named questionTypeID that specifies which type of input needs to be created.
questionTypesID 1 - Spinner
questionTypesID 2 - Multi Select Spinner
questionTypesID 5 - EditText

So first i retrieve the question list and as per the questionTypeID appropriate widgets need to be created

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900