Reputation: 339
I have a list of questions that require different elements for the answers. Specifically question 1 may have checkbox
answers question 2 may have a spinner
with an array
of answers and question 3 may have an EditText
so the user can type the answer.
The problem is until the data is passed to the application you don't know how many questions there will be and you don't know what format the answers will be in. So one survey might have 10 questions with 3 checkboxes, 3 spinners, and 4 edit texts and the next one might have 50 questions with half edit text half check boxes. Until the data comes in from a remote server with the survey questions/answers there is no way to know.
The questions will always be text views. However I am stumped on how to get the layout to inflate in real time answers with check boxes, spinners etc.The possible answers will have some type of identifier letting the application know they are check boxes, spinners etc.
If your still lost here is a quick and crude diagram
survey1
Q1) TextView A1) 3 CheckBox answers
Q2) TextView A2) 10 Spinner Answers
Q3) TextView A3) EditText
now survey one is done and survey 2 is coming into the same activity, layout, fragment or whatever and it has this data
survey2
Q1) TextView A1) 3 CheckBox answers
Q2) TextView A2) 3 CheckBox answers
Q3) TextView A3) 3 CheckBox answers
Q4) TextView A4) 3 EditText answers
Q5) TextView A5) 3 EditText answers
Q6) TextView A6) 3 EditText answers
So you see the problem? I can't simply make a layout with a listview, or 3 checkboxes or 3 textViews and feed them data because I don't even know what types of fields or how many I'll need. I know this is not an easy question and may need more information. It's just something I've been trying to wrap my head around the logic behind and I simply don't know of any way to do this.
Upvotes: 0
Views: 175
Reputation: 17444
I think you should use a ListView
for the questionnaire wherein each item is a single question. Each kind of question gets its own layout xml file. This gets you most of the functionality you need, so it's a good place to begin from.
For those question types where the number of response options is variable, you're going to have to programmatically add extra fields to or remove excess fields from the view before returning it from the ListAdapter
's getView method. You can inflate or new
some views and LayoutParameters
in Java code and add them to a LinearLayout
that you designed into the question type specific xml view.
In rough outline, your adapter's getView()
method would look something like this:
public View getView(int pos, View coV, ViewGroup parent) {
View result = //inflate view for question type
if (/* questiontype has variable number of answers */ ) {
LinearLayout answersParent = (LinearLayout) result.findViewById(/* id */);
for (int i = 0, max = question.numAnswers(); i < max; i++) {
View answerView = //inflate or new the View for the answer type
// Configure answerview
answersParent.addView(answerView);
}
}
return result;
}
Upvotes: 1