8

We are currently working on project where a web user creates a survey which has cascading questions (cascading means questions that have few answers and depend on those answers rest of the questions changes) and then mobile users should get this survey in their android app. Sample JSON structure we thought is as follows:

{
  "status": "1",
  "tabs": [
    "Farmer Information",
    "Signature",
    "Crop Details",
    "Land Parcel"
  ],
  "survey":[
    {
      "type": "TextView",
      "cascade": "0",
      "value": "What is your name?",
      "survey": ""
    },
    {
      "type": "TextView",
      "cascade": "0",
      "value": "What is your age?",
      "survey" : ""
    },
    {
      "type": "RadioButtonGroup",
      "cascade": "1",
      "value": "Do you have kids?",
      "survey" : [
        {
          "type": "TextView",
          "cascade": "1",
          "value": "YES",
          "survey": [
            {
              "type": "TextView",
              "cascade": "1",
              "value": "How many of them below 18?",
              "survey": ""
            },
            {
              "type": "TextView",
              "cascade": "0",
              "value": "How many of them are girls?",
              "survey" : ""
            },
            {
              "type": "TextView",
              "cascade": "1",
              "value": "Where do you live in?",
              "survey": ""
            },
            {
              "type": "TextView",
              "cascade": "0",
              "value": "How long you were there?",
              "survey" : ""
            }
          ]
        },
        {
          "type": "TextView",
          "cascade": "0",
          "value": "NO",
          "survey" : [
            {
              "type": "TextView",
              "cascade": "1",
              "value": "Where do you live in?",
              "survey": ""
            },
            {
              "type": "TextView",
              "cascade": "0",
              "value": "How long you were there?",
              "survey" : ""
            }
          ]
        }
      ]
    }
  ]
}

Is there anyway to achieve such a thing? What are the most suitable library which can be used for this scenario? We tried json2view, proteus. From all those we can pass a json and load the view but if there are cascading questions non of them can use.

Furthermore elaborating in the question via an example: Suppose the user is given the question of Do you have any have kids?. This question has possible two answers. Yes & No depending upon the answer which user give others questions have to be loaded dynamically.

4

1 回答 1

2

这可能为时已晚,但在这里。这可以在proteus.

哲学:你会如何在原生 Android 中做到这一点?

通过创建一个自定义视图,让我们调用它SurveyView具有 3 个自定义属性

  1. status: 初始条件
  2. cascade: 检查条件
  3. survey: 子数组SurveyView

因此,在 Native Android 中,会有一个方法setStatus()在选择答案时被调用。这将设置status子调查视图。孩子将根据status设置在他们身上的条件检查新的。如果匹配,则为visibleelse gone

在此之后,只需将其注册为 proteus 的自定义视图。在没有自定义视图的情况下,有一种稍微复杂一点的方法(通过使用类似的函数绑定。

{
  "visibility: "@{fn:eq(@{data.status}, 0)}"
}

data.status从父级向下传播的状态在哪里,并且0是级联条件。你明白了。

fn:eq是一个内置函数。在此处查看所有可用功能

您也可以创建和注册自己的自定义函数。

PS为什么不反应原生?

PPSproteus在与 proteus 相关的问题中使用标签,这就是我错过这个的原因。

于 2018-04-30T03:24:04.683 回答