Home > OS >  Filter in nested list json return only one value
Filter in nested list json return only one value

Time:12-01

My JSON is something like this :

[{
    "id": 0001,
    "date": "2021-11-16T20:44:43",
    "category": [
      01
    ],
   "startdate": "2021-12-11"
}
{
    "id": 0002,
    "date": "2021-11-16T20:44:43",
    "category": [
      01,03
    ],
   "startdate": "2021-12-11"
}
]

I want to filter by category. Here is my code in my adapter :

 var list = mutableListOf<MyDataItem>()
 var listFiltered = mutableListOf<MyDataItem>()

 fun setDataList(list: List<MyDataItem>) {
        this.list = list.toMutableList()
        var  listFiltered = list?.filter { item ->
            item.category.all { it == "01" }
        }.toMutableList()
        notifyDataSetChanged()
    }

override fun onBindViewHolder(holder: ListViewHolder, position: Int) {

        val currentItem = listFiltered[position]
        holder.binding.txtDate.text = currentItem.startdate
        holder.binding.txtTitle.text = currentItem.title.rendered

    }

I get only the first value... where did I get it wrong ??

CodePudding user response:

You are supposed to use .any instead of .all

All will require all elements to be "01" -> which is true for first item

Any will return true if any of the given elements matches

  • Related