Dynamically load cascading Questions in Android View - android

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.

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
status: initial condition
cascade: the check condition
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.

Related

Android library to dynamically generate views from a given user file

I am trying to find a tool to automatically generate a set of view elements in the fragment.
The idea is to have a user input template file (xml or json), where the user can define the simple fields that he wants to see displayed in the fragment such as TextView, EditText, Buttons or spinner menus.
Then, the app can read that file and load/generate automatically the fields and show them in the fragment.
For example, a user defines the following json file, here define 2 TextFields and one checkbox with 2 options:
{
"sections": [
{
"rows": [
{
"items": [
{
"componentType": "TextField",
"inputType" "text",
"var": "user name"
}
]
}
],
"itemRank": "1",
"title": "User Login Name",
"shortTitle": "login"
},
{
"rows": [
{
"items": [
{
"componentType": "TextField",
"inputType" "float",
"var": "0.0",
"min": "0.0",
"max": "100.0",
"format":"3.3f"
}
]
}
],
"itemRank": "2",
"title": "Weather Temperature",
"shortTitle": "temp"
},
{
"rows": [
{
"items": [
{
"componentType": "multiSelect",
"inputType" "float",
"options": [
{
"text": "Checkbox option 1",
"var": "option1",
"selected": "false"
},
{
"text": "Checkbox option 2",
"var": "option2",
"selected": "false"
}
]
}
]
}
],
"itemRank": "3",
"title": "Weather Temperature",
"shortTitle": "temp"
},
"name": "My json template view elements",
"title": "My report"
}
Having that json file as an input, generate these fields to attach them on the fragment as views and process them accordingly.
Is there a library that can do that?

How to create Form from Json schema and Ui schema in Android Kotlin

How to create Form from Json schema and Ui schema in Android Kotlin? I am new to mobile development and am a little bit confused how to do this in Jetpack Compose app. I find many ways to create a form in React. For Android Kotlin there is https://github.com/ellykits/neat-form , but it requires a different schema structure of Json schema. I have Json schema as below:
{
"title": ".....",
"description": "",
"type": "object",
"required": [
"description",
"violationType"
],
"properties": {
"videoLink": {
"type": "string",
"title": "Видео",
"default": "https://....."
},
"description": {
"type": "string",
"title": "....."
},
"violationType": {
"type": "string",
"title": ".......?",
"enum": [
".....",
".....",
".....",
".....",
"....."
]
}
}
}
and Ui schema:
{
"description": {
"ui:widget":"textarea"
},
"violationType": {
"ui:widget":"radio"
},
"videoLink": {
"ui:widget": "customVideoWidget"
},
"ui:order": [
"videoLink",
"description",
"violationType"
]
}
And could you advice me what can I read to understand how to work with Json schema to create forms?
Thank you very much for advance.
I think it can be done by creating a factory class and pass form item and it will decide with FormItemView to return.
FormItemView is a view extending for example MaterialTextInput and sets input type and placeholder, etc that it gets from json.

How to use Nested Arraylist with recyclerView?

I wanted to populate a nested ArrayList.
Below is the JSON response from the server. Now I want sectionName as the headerView and sectionItems as the Child view of that header. So That, this layout will look like categorized items.
I have researched the same but couldn't able to find any easy and efficient solution. I don't want to create a separate flatten list because that would cost me time.
Now, How can I achieve this layout.
PS: I don't want to use any external library.
"data": [
{
"sectionName": "Fruits",
"sectionItems": [
{
"id": "123",
"name": "Apple",
"image": "url",
"price": "120",
"quntity": "600gm"
},
{
"id": "234",
"name": "Orange",
"image": "url",
"price": "340",
"quntity": "2kg"
},
{
"id": "334",
"name": "Banana",
"image": "url",
"price": "340",
"quntity": "2kg"
}
]
},
{
"sectionName": "Paints",
"sectionItems": [
{
"id": "345",
"name": "Red",
"image": "url",
"price": "120",
"quntity": "1l"
},
{
"id": "346",
"name": "White",
"image": "url",
"price": "120",
"quntity": "1l"
},
{
"id": "347",
"name": "Purple",
"image": "url",
"price": "120",
"quntity": "1l"
}
]
}
EDIT: I have implemented a nested recycler view but couldn't able to render all the child element. there are more than 80 items on the nested list but I can see only the 18.
To answer your question, in my opinion the best idea here is to flatten the list. There are other possibilities but do they really fit the design of the RecyclerView? I don't think so. Some time ago I wrote a blog post to show how easy you can achieve sectioned RecyclerView:
https://brona.blog/2020/06/sectioned-recyclerview-in-three-steps/

Dynamic JSON parsing

I am a newbie to android. I am stuck at JSON parsing.
Following is my JSON, in this, the type of menu categories will be dynamic, and I want to arrange this JSON in sectioned recycler view.
Please help me out.
This is my JSON
{
"menu_items": {
"Plat": [{
"menu_name": "jambon à l'os",
"menu_cost": "18.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
},
{
"menu_name": "tacos",
"menu_cost": "6.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
}
],
"Entrée": [{
"menu_name": "avocat",
"menu_cost": "7.50",
"menu_count": "1",
"category": "Entrée",
"menu_tax": "5.50",
"code": "€"
}]
},
"order_data": {
"order_id": "278",
"order_number": "REF-5d5e6b86",
"created_at": "1566468998",
"instructions": "",
"fullname": "Rohan Chitnis",
"phonenumber": "7610771871",
"useraddress": "Brest, France",
"image_path": "https://restau-at-home.bzh/uploads/attachments/Jellyfish_2019_08_21_10_49_21.jpg",
"wp": "pune",
"code": "€",
"promocode": null,
"promo_value": null,
"promo_type": null
},
"order_details": {
"total_items": "3",
"order_total": "31.50"
},
"promo_applied": "0",
"error": {
"code": "13",
"status": "200",
"message": "Ok"
}
}
Check this out. Create a generic response class. Extend your classes from that class. To do this properly you can look at Java Generics and Inheritance topics if you are using Java. And then search about Sectioned RecyclerView in Android. Here is a library for this.

How to fetch data from a JSON file in Android Studio

I am trying to make an app showing info from an API. I have searched everywhere but I didn't find an answer that solve my problem!
Here is an example from the JSON file:
{
"status": 200,
"data": {
"date": "2018-10-12T00:00:00.000Z",
"featured": [
{
"id": "5bbdc3abdf819b65ea6b733b",
"name": "Name",
"price": "1",
"priceIcon": "priceicon",
"priceIconLink": "price.png",
"images": {
"icon": "icon.png",
"png": false,
"gallery": false,
"featured": false
},
"rarity": "rarity",
"type": "type",
"readableType": "Type"
}
]
}
}
How can I fetch the name, price, icon, rarity and readableType?
Please don't mark it as duplicate because I haven't found the answer that I am searching for.
Check this tutorial, no one is going to code it for you, also your question has been made 10000 times
Tutorial

Categories

Resources