I'm having hard times trying to parse the following JSON and save it in a list.
{
"statut": "Success",
"rubrique": [
{
"rubrique": "Accueil",
"position": "1"
},
{
"rubrique": "Films",
"position": "2"
},
{
"rubrique": "Séries",
"position": "3"
},
{
"rubrique": "Sketchs",
"position": "4"
},
{
"rubrique": "Musique",
"position": "5"
}
]
}
I tried several methods then the below code worked,exept my list only contains one item: Musique
val queue = Volley.newRequestQueue(activity)
val stringRequest = StringRequest(Request.Method.GET, endpoint,
Response.Listener<String> { response ->
val stringResponse = response.toString()
val jsonObj = JSONObject(stringResponse)
val jsonArray: JSONArray = jsonObj.getJSONArray("rubrique")
for (i in 0 until jsonArray.length()) {
val innerBlock: JSONObject = jsonArray.getJSONObject(i)
val item: RubriqueItem = RubriqueItem()
item.position = innerBlock.getString("position")
item.rubrique = innerBlock.getString("rubrique")
categories?.add(item)
}
binding?.viewPagerMain?.adapter = BaseCategoriesFragmentPagerAdapter(
parentFragmentManager, categories)
binding?.tabLayout?.setupWithViewPager(binding?.viewPagerMain)
},
Response.ErrorListener {error -> error.printStackTrace() })
queue.add(stringRequest)
categories is ArrayList which I need to pass to the ViewPager's adapter. Tried to figure out a way to pass the index but I'm new to Kotlin can't figure out yet
Any hint is welcome