Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically load cascading Questions in Android View

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.

like image 539
Selaka Nanayakkara Avatar asked Dec 20 '17 06:12

Selaka Nanayakkara


1 Answers

This may be too late, but here goes. This can be done in proteus.

Philosophy: How would you do it in Native Android?

By creating a custom view, let call it SurveyView which has 3 custom attributes

  1. status: initial condition
  2. cascade: the check condition
  3. survey: array of child SurveyView

So, in Native Android, there would be a method setStatus() which will be called when an answer is selected. This will set the status of the child survey views. The child will check the new status against the condition set on them. If it matches, it is visible else gone.

After this simply register this as a custom view with proteus. There is a slightly more complicated way of doing this without a custom view (by using function bindings in like so.

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

Where data.status is the status propagated down from the parent and 0 is the cascade condition. You get the drift.

fn:eq is a built-in function. Check here for all available functions here

You can create and register your own custom functions too.

P.S. why not react native?

P.P.S Use proteus tag in questions related to proteus, this is why I missed this.

like image 66
vader Avatar answered Nov 15 '22 05:11

vader