I am new to Kotlin coding and I can't resolve this issue.
I have a Json list which looks like this :
[
{
"id": 01,
"title": {
"rendered": "MY TITLE 1"
},
"category": [52],
},
{
"id": 02,
"title": {
"rendered": "MY TITLE 2"
},
"category": [52],[64]
},
{
"id": 03,
"title": {
"rendered": "MY TITLE 3"
},
"category": [64]
}
...
]
I managed to code a list adapter with a recyclerview to display a list with each titles. Now I want another recyclerview with only each categories but all I can achieve is a list of categories with duplicates. I added a 'distinct()' to my list but I can't iterate it through the adapter.
Here is my code :
In the adapter :
override fun onBindViewHolder(holder: ListCatViewHolder, position: Int) {
val currentItem = mylist.flatMap { it.category}
val list2 = currentItem.toList()
val test = list2[position]
holder.binding.txtCat.text =test.toString()
}
The result in the emulator is: enter image description here
If I add a .distinct() to get rid of the duplicates like this :
override fun onBindViewHolder(holder: ListCatViewHolder, position: Int) {
val currentItem = mylist.flatMap { it.category}
val list2 = currentItem.toList().distinct()
val test = list2[position]
holder.binding.txtCat.text =test.toString()
}
I've got this error message : java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
but if I change 'val test = list2[position]' to 'val test = list2', the result is :
Duplicated entries are removed but I cannot iterate it in the adapter ???
Thanks for your lights !