How to: Keep track of Progress on questionnaire

匆匆过客 提交于 2019-12-25 00:02:34

问题


Good day all, I am working on a practice app that involves fragments, keeping track of data, and SharedPreferences, Here is the scenario, I have a questionnaire-like app, it will ask the user 15 questions but for the sake of this example i'm going to say five(5) and use random questions with yes and no buttons as options.It will look something like this:

|Question 1: Are you a citizen?|

yes/no

|Question 2: Are you human?|

yes/no

|Question 3: Do you sleep a lot?|

yes/no

|Question 4: Do you think you will need help?|

yes/no

|Question 5: Do you often say bye?|

yes/no

|----------------------------|
|                            |
|                            |
|                            |
|   fragment displayed here  |
|                            |
|                            |
|----------------------------|
|                            |
|yes                  no     |
|----------------------------|

figure 1- Shows how the app looks-both linear layouts split into two

Now, each question is in its own Fragment-displayed in the "fragments displayed here" section seen in figure 1. The yes and no buttons in the bottom half are NOT supposed to be stagnant but in fact connected to each question. Once the user clicks an option from the buttons, it will display another question. Note: The questions are not a list, the selection of a particular (yes/no) will in turn display the corresponding question.

So for example, Question1:yes will take you to Question3, answering no will take you to Question 4 and so on. Now, each question has the option or chance to give a document or say "You will need document.doc" based on the selection. I need to find a way to keep track of what answers the user picked, and upon calculating those answers along with the chance of displaying a document i will show a results page telling the user: "You will need the following documents: documents.doc, hello.doc etc". The results page shows what all the user chose. e.g

Question1:yes
Question2:no
Question3:yes
Question4:yes
Question5:yes
Result: You will need documents: documents.doc, hello.doc, bye.doc, yes.txt

If anything is not clear, please let me know. Basically now all I want to know is how i can accomplish this task, i would really appreciate it if someone would work with me on getting this done as soon as possible. Thanks SO much in advance to the community.


回答1:


If I get the core of your problem right, your have different activities and views (for each question) that need to be consolidated to a central state, collecting all answers and the documents determined by them. So the solution idea is to have a domain object that holds the answers and the logic to determine documents from given answers. Something like

class Questionnaire {
   private boolean answer1;
   private boolean answer2;
   ...
   public boolean setAnswer1(boolean answer) ...
   ...
   public String getNeededDocuments() {
       // determine documents from values of answer1, answer2, ...
   }
}

There are several ways to implement this:

  • The easiest way would be to make all attributes and methods of the class shown above static. But it may happen that those values get lost, cf. Lifetime of a static variable in Android.

  • Another way is to hold one object of the class in your application (extension of the Application class). To make sure the answer values don't get lost when the app is swapped out, each activity should, in its onSaveInstanceState method call a method of Questionnaire that serializes the answers, and conversely a call to deserialized them in its onCreate method.

  • The luxury version from the user's point of view would be to store the answers in a database where they'll survive everything but physical destruction of the device.

Which way to go will also depend on the usage scenario. If you image the user to answer all those questions without interruption, saving instances might be unneeded. The more effort the user will put into that questionnaire, the more effort your app should take to persist the answers.



来源:https://stackoverflow.com/questions/36502674/how-to-keep-track-of-progress-on-questionnaire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!