Advertisement
arijulianto

search

Jul 24th, 2023
3,844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.93 KB | None | 0 0
  1. -- Adapter
  2. fun filterList(filterList: MutableList<Piutang>) {
  3.         // below line is to add our filtered
  4.         // list in our course array list.
  5.         listData = filterList
  6.         // below line is to notify our adapter
  7.         // as change in recycler view data.
  8.         notifyDataSetChanged()
  9.     }
  10.  
  11. -- Activity
  12. private fun initView(){
  13.         with(binding){
  14.             adapter = AdapterPiutang()
  15.             lvData.layoutManager = LinearLayoutManager(baseContext)
  16.             lvData.adapter = adapter
  17.  
  18.             txtSearch.addTextChangedListener(object : TextWatcher {
  19.                 override fun beforeTextChanged(
  20.                     charSequence: CharSequence,
  21.                     i: Int,
  22.                     i1: Int,
  23.                     i2: Int
  24.                 ) {
  25.                 }
  26.  
  27.                 override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
  28.                 override fun afterTextChanged(editable: Editable) {
  29.                     //after the change calling the method and passing the search input
  30.                     filter(editable.toString())
  31.                 }
  32.             })
  33.  
  34.             btnAdd.setOnClickListener{
  35.                 val intent = Intent(baseContext, PiutangInputActivity::class.java)
  36.                 startActivity(intent)
  37.             }
  38.         }
  39.     }
  40.  
  41.     private fun filter(text: String) {
  42.         //new array list that will hold the filtered data
  43.         val filterdNames = mutableListOf<Piutang>()
  44.  
  45.         //looping through existing elements
  46.         for (s in listData) {
  47.             //if the existing elements contains the search input
  48.             if (s.nama?.lowercase()!!.contains(text.lowercase())) {
  49.                 //adding the element to filtered list
  50.                 filterdNames.add(s)
  51.             }
  52.         }
  53.  
  54.         //calling a method of the adapter class and passing the filtered list
  55.         adapter.filterList(filterdNames)
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement