I have a question that how can I sort Arraylist in on the basis of boolean parameter and only display true items. datasource.sortByDescending { it.favorite }
Here I am getting sorted array in which I got true items on top and false items on bottom. What I want is that it only display true items and not display false items.
I hope I am clear with my question.
Full Code is below:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rvlist.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val datasource = getDataSource()
val beneficiaryAdapter = BeneficiaryAdapter(datasource)
rvlist.adapter = beneficiaryAdapter
btnAll.setOnClickListener{
btnFavorite.setBackgroundColor(Color.BLUE)
btnAll.setBackgroundColor(Color.GREEN)
btnSortByFrequency.setBackgroundColor(Color.BLUE)
btnSortByName.setBackgroundColor(Color.BLUE)
rvlist.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val datasource = getDataSource()
val beneficiaryAdapter = BeneficiaryAdapter(datasource)
rvlist.adapter = beneficiaryAdapter
}
btnFavorite.setOnClickListener{
btnFavorite.setBackgroundColor(Color.GREEN)
btnAll.setBackgroundColor(Color.BLUE)
btnSortByFrequency.setBackgroundColor(Color.BLUE)
btnSortByName.setBackgroundColor(Color.BLUE)
rvlist.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val datasource = getDataSource()
datasource.sortByDescending { it.favorite }
val beneficiaryAdapter = BeneficiaryAdapter(datasource)
rvlist.adapter = beneficiaryAdapter
}
btnSortByFrequency.setOnClickListener{
btnFavorite.setBackgroundColor(Color.BLUE)
btnAll.setBackgroundColor(Color.BLUE)
btnSortByFrequency.setBackgroundColor(Color.GREEN)
btnSortByName.setBackgroundColor(Color.BLUE)
rvlist.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val datasource = getDataSource()
datasource.sortByDescending { it.frequency }
val beneficiaryAdapter = BeneficiaryAdapter(datasource)
rvlist.adapter = beneficiaryAdapter
}
btnSortByName.setOnClickListener{
btnFavorite.setBackgroundColor(Color.BLUE)
btnAll.setBackgroundColor(Color.BLUE)
btnSortByFrequency.setBackgroundColor(Color.BLUE)
btnSortByName.setBackgroundColor(Color.GREEN)
rvlist.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val datasource = getDataSource()
datasource.sortBy { it.name }
val beneficiaryAdapter = BeneficiaryAdapter(datasource)
rvlist.adapter = beneficiaryAdapter
}
}
private fun getDataSource():ArrayList<beneficiary>{
val list = ArrayList<beneficiary>()
list.add(beneficiary(1, "Adbul Malik", "12346564555124", "Meezan",true,5))
list.add(beneficiary(2, "Naeem Ali", "6454886614281", "HBL",true,5))
list.add(beneficiary(3, "Ali Raza", "456467864568465", "Soneri",false,4))
list.add(beneficiary(4, "Talha Abid", "46873456465464564", "Allied",false,1))
list.add(beneficiary(5, "Hassan Ali", "16456464564646", "Meezan",false,5))
list.add(beneficiary(6, "Shahid Khan", "78946756195845648", "HBL",true,3))
list.add(beneficiary(7, "Umer Riaz", "79464567754379", "Soneri",false,6))
list.add(beneficiary(8, "Zeeshan Farooq", "797536187897465", "Allied",false,1))
list.add(beneficiary(9, "Qasim Ali", "551988763478565746765", "MCB",false,1))
list.add(beneficiary(10, "Farooq Haider", "81786523478465010", "Punjab",false,2))
return list
}
}
CodePudding user response:
You can basically filter your array list as below
getDataSource().filter { it.favorite }
You can update your code as below:
btnFavorite.setOnClickListener{
btnFavorite.setBackgroundColor(Color.GREEN)
btnAll.setBackgroundColor(Color.BLUE)
btnSortByFrequency.setBackgroundColor(Color.BLUE)
btnSortByName.setBackgroundColor(Color.BLUE)
rvlist.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val beneficiaryAdapter = BeneficiaryAdapter(getDataSource().filter { it.favorite })
rvlist.adapter = beneficiaryAdapter
}
This won't update your dataSource
variable after you tried to change. You have to put it into new variable or directly use it on where you are going to use
val datasource = getDataSource()
datasource.sortByDescending { it.frequency }