阵列仅打印最后一个回收站视图

时间:2019-07-11 11:24:17

标签: arrays json kotlin

我有一个如下所示的响应,我想从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
}

我应该如何for循环才能在textview中获取问题。我能够获取问题数组,但仅在textview中打印最后一个值。我想在textview中打印所有值

1 个答案:

答案 0 :(得分:0)

首先创建本地问题列表,然后可以使用

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 
            }

使用上述for循环,您将能够从问题数组中获取问题。然后,您可以使用字符串生成器或追加来获取最终的字符串。

并避免使用!! 。如果您的数组为null,则可能会导致null指针异常。改为使用? 。