简体   繁体   English

如何:跟踪调查表的进度

[英]How to: Keep track of Progress on questionnaire

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: 一切顺利 ,我正在开发一个涉及片段,跟踪数据和SharedPreferences的练习应用程序,在这种情况下,我有一个类似于问卷的应用程序,它将向用户询问15个问题,但出于本示例的目的我要说五(5),并使用带有是和否按钮的随机问题作为选项。它将看起来像这样:

|Question 1: Are you a citizen?| |问题1:您是公民吗?|

yes/no 是/否

|Question 2: Are you human?| |问题2:您是人吗?|

yes/no 是/否

|Question 3: Do you sleep a lot?| |问题3:您睡得很多吗?

yes/no 是/否

|Question 4: Do you think you will need help?| |问题4:您认为需要帮助吗?

yes/no 是/否

|Question 5: Do you often say bye?| |问题5:您经常说再见吗?|

yes/no 是/否

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

figure 1- Shows how the app looks-both linear layouts split into two 图1-显示应用程序的外观-线性布局均分为两部分

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. 现在,每个问题都位于其自己的Fragment中-显示在图1的“此处显示的片段”部分中。下半部分的yes和no按钮不应停滞,而是实际上与每个问题相关。 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. 例如,问题1:是将带您进入问题3,回答否将带您进入问题4,依此类推。 Now, each question has the option or chance to give a document or say "You will need document.doc" based on the selection. 现在,每个问题都有选择权或机会根据您的选择给出文件或说“您将需要document.doc”。 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". 我需要找到一种跟踪用户选择的答案的方法,在计算出这些答案以及显示文档的机会后,我将显示一个结果页面,告诉用户:“您将需要以下文档:documents.doc ,hello.doc等”。 The results page shows what all the user chose. 结果页面显示所有用户选择的内容。 eg 例如

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. 非常感谢社区。

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 . 最简单的方法是使上面显示的类的所有属性和方法static But it may happen that those values get lost, cf. 但是,这些值可能会丢失,请参见。 Lifetime of a static variable in Android . Android中静态变量的生命周期

  • Another way is to hold one object of the class in your application (extension of the Application class). 另一种方法是在应用程序中保留该类的一个对象( Application类的扩展)。 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. 为确保在换出应用程序时不会丢失答案值,每个活动都应在其onSaveInstanceState方法中调用Questionnaire方法以序列化答案,然后在其onCreate方法中调用反序列化它们。

  • 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. 用户对调查表投入的精力越多,您的应用就应花费更多的精力来保持答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM