I am learning Kotlin and am trying to filter some data coming straight from a (room) database into my adapter to display them.
Here's my code (from within the fragment, containing a recycleview with adapter):
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = LatestReleaseListAdapter {
}
binding.recyclerView.layoutManager = LinearLayoutManager(this.context)
binding.recyclerView.adapter = adapter
viewModel.allItems.observe(this.viewLifecycleOwner) { items ->
//Here I'd like to remove the items that has been seen already
items.filter { item -> !item.hasBeenSeen }
items.let {
adapter.submitList(it)
}
}
The code is quite straight forward as you can see. I am simply trying to filter the element of the list where the boolean "hasBeenSeen"is true. I only want to show the ones where the boolean "hasBeenSeen"is false. How can I achieve that?
The call to filter
will filter the items and return a new collection that will contain just the filtered items.
So you could do something like this
val filteredItems = items.filter { item -> !item.hasBeenSeen }
adapter.submitList(filteredItems)
Or keep it short
adapter.submitList(items.filter { item -> !item.hasBeenSeen })