Hi everyone.
I am new to kotlin. My group is going to build a questionnaire app with kotlin.
I have a google spread sheet on the google drive and I am going to get the data from the spread sheet and show it in the app question by question, page by page. I have wrote the code to get the data in Json format and parse it with the function parseItem. However, I have meet two problem when I move on.
i) I have set the numbers of val such as queContent, ChoiceA… in the parseItems, however, I cannot call it out when I am out of the function parseItems. It needs member declaration again. I am wonder is there any way for me to call back all my val define in the parseItems when I am out of it.
ii) Is there any way that I can show it page by page instead of list them all out in a single page?
Please help if anyone of you have done somethings similar before. Many Many thanks!!
Belows are the parseItems code:
class ListItem : AppCompatActivity() {
private lateinit var listView: ListView
private lateinit var adapter: ListAdapter
private var loadingsyb: ProgressDialog1? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.list_item)
listView = findViewById<ListView>(R.id.lv_items)
getItems()
}
private fun parseItems(jsonResponse: String) {
val list = ArrayList<HashMap<String, String>>()
val jobj = JSONObject(jsonResponse)
val jarray = jobj.getJSONArray("items")
var corrAns = Array(jarray.length()) { i -> " $i" }
try {
for (i in 0 until jarray.length()) {
val jo = jarray.getJSONObject(i)
val queNo = jo.getString("queNo")
val queContent = jo.getString("queContent")
corrAns[i] = queContent
val choiceA = jo.getString("choiceA")
val choiceB = jo.getString("choiceB")
val choiceC = jo.getString("choiceC")
val choiceD = jo.getString("choiceD")
val item = HashMap<String, String>()
item["queNo"] = queNo
item["queContent"] = queContent
item["choiceA"] = choiceA
item["choiceB"] = choiceB
item["choiceC"] = choiceC
item["choiceD"] = choiceD
list.add(item)
}
}
catch (e: JSONException) {
e.printStackTrace()
println("$corrAns")
}
adapter = SimpleAdapter(
this, list, R.layout.list_item_row,
arrayOf("queContent", "choiceA", "choiceB", "choiceC", "choiceD")
intArrayOf(R.id.tv_queContent, R.id.rb_ansA, R.id.rb_ansB, R.id.rb_ansC, R.id.rb_ansD)
)
listView.adapter = adapter
loadingsyb?.dismiss()
}