arrays - Array 仅打印最后一项回收站视图
问题描述
我有一个如下所示的响应,我想从 Quetions 数组中打印数组的值。我只能打印最后一个值。请帮忙..
JSON
[
{
"id": 66,
"module_id": 1,
"module_name": "Medical",
"parent_id": 14,
"label": "Burns ",
"description": null,
"img_id": null,
"questions": [
{
"question": "Take a picture",
"input_type": "text",
"symptom_id": 66,
"question_id": 294,
"symptom_name": "Burns ",
"input_type_data": null
},
{
"question": "Give an accurate description of the accident",
"input_type": "text",
"symptom_id": 66,
"question_id": 295,
"symptom_name": "Burns ",
"input_type_data": null
},
{
"question": "What caused the burns?",
"input_type": "button",
"symptom_id": 66,
"question_id": 296,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Fire"
},
{
"text": "Scorching-metal"
},
{
"text": "Boiling-water"
},
{
"text": "Steams-or-vapours"
},
{
"text": "Sunbeams"
},
{
"text": "Other-agents"
}
]
},
{
"question": "How long did the exposure last?",
"input_type": "text",
"symptom_id": 66,
"question_id": 297,
"symptom_name": "Burns ",
"input_type_data": null
},
{
"question": "Did he inhale boiling gas or vapours?",
"input_type": "button",
"symptom_id": 66,
"question_id": 298,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Yes"
},
{
"text": "Noo"
}
]
},
{
"symptom_id": 66,
"question_id": 299,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Conscious"
},
{
"text": "Unconscious"
}
],
"question": "Was he conscious or unconscious?",
"input_type": "button"
},
{
"question": "Did the patient get any traumatism because\nof failing down?",
"input_type": "button",
"symptom_id": 66,
"question_id": 300,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Yes"
},
{
"text": "Noo"
}
]
},
{
"question": "Does he have a cough or breathing difficulties?",
"input_type": "button",
"symptom_id": 66,
"question_id": 301,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Yes"
},
{
"text": "Noo"
}
]
},
{
"question": "Does he have nausea or vomit?",
"input_type": "button",
"symptom_id": 66,
"question_id": 302,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Yes"
},
{
"text": "Noo"
}
]
},
{
"question": "Does he was like fainting?",
"input_type": "button",
"symptom_id": 66,
"question_id": 303,
"symptom_name": "Burns ",
"input_type_data": [
{
"text": "Yes"
},
{
"text": "Noo"
}
]
}
]
}
]
我的逻辑
for (str in symptomsData!![position].questions!!)
{
var ans=str.question
holder.question.text = ans
}
我应该如何循环以获取 textview 中的问题。我能够获取问题数组但仅打印 textview 中的最后一个值。我想打印 textview 中的所有值
解决方案
First create a local list of questions .Then you can use
val list = ArrayList<questions>()
//Add your current position questions to arraylist
list.addall(symptomsData!![position].questions)
for (i in questions. indices){
//append your last question here
holder.question.text = questions[i].question
}
With above for loop , you will be able to get the question from the questions array . Then you use string builder or append to get your final string .
And avoid using !! . This may result in null pointer exception , if your array is null . Instead use ? .
推荐阅读
- android - 替换 BottomNavigationView 中的选项卡
- mysql - laravel 中的批量插入或更新
- javascript - Typescript自动完成在VSCode中不起作用
- vb.net - 从 json 模式生成 vb 类的有效方法是什么?
- .htaccess - .htaccess Header 命令在使用转义 URL 时产生错误 500
- batch-file - 用批处理删除文本文件的行
- amazon-web-services - 具有跨账户 S3 访问的 AWS CodeDeploy 失败
- sqlite - sqflite颤动中的AND语句
- java - 我如何有条件地阻止用户导航到 Eclipse E4 RCP 应用程序中的不同视图/部分?
- html - HTML 录音权限