Home > Enterprise >  How to show items between 51 to 100 only from list on RecyclerView
How to show items between 51 to 100 only from list on RecyclerView

Time:12-29

I want to show items between 51 to 100 from a list containing a size of 200. How can I do that on a custom Adapter. Thank you.

CodePudding user response:

val list = (1..200).toList() //creates a list of integers from 1 to 200 
val sublist = list.subList(50, 100) // gets the sublist of items between indices 50 and 100 (inclusive)

for (item in sublist) {
    println(item) // prints the items in the sublist
}

CodePudding user response:

You can use list.filterIndexed to filter items you want according to index.

CodePudding user response:

Here getList is your complete list of 200 items

private val originalList = getList()
private val dataList = ArrayList<String>()
private lateinit var customAdapter: CustomAdapter

In your onCreate(),

dataList.addAll(originalList)
customAdapter = CustomAdapter(dataList)
binding.recyclerView.adapter = adapter

Now, for your selection (maybe on button click)

dataList.clear()
dataList.addAll(originalList.subList(51, 100))
customAdapter.notifyDataSetChanged()
  • Related