首页 > 解决方案 > 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 中的所有值

标签: arraysjsonkotlin

解决方案


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 ? .


推荐阅读