Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Adapter
- fun filterList(filterList: MutableList<Piutang>) {
- // below line is to add our filtered
- // list in our course array list.
- listData = filterList
- // below line is to notify our adapter
- // as change in recycler view data.
- notifyDataSetChanged()
- }
- -- Activity
- private fun initView(){
- with(binding){
- adapter = AdapterPiutang()
- lvData.layoutManager = LinearLayoutManager(baseContext)
- lvData.adapter = adapter
- txtSearch.addTextChangedListener(object : TextWatcher {
- override fun beforeTextChanged(
- charSequence: CharSequence,
- i: Int,
- i1: Int,
- i2: Int
- ) {
- }
- override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
- override fun afterTextChanged(editable: Editable) {
- //after the change calling the method and passing the search input
- filter(editable.toString())
- }
- })
- btnAdd.setOnClickListener{
- val intent = Intent(baseContext, PiutangInputActivity::class.java)
- startActivity(intent)
- }
- }
- }
- private fun filter(text: String) {
- //new array list that will hold the filtered data
- val filterdNames = mutableListOf<Piutang>()
- //looping through existing elements
- for (s in listData) {
- //if the existing elements contains the search input
- if (s.nama?.lowercase()!!.contains(text.lowercase())) {
- //adding the element to filtered list
- filterdNames.add(s)
- }
- }
- //calling a method of the adapter class and passing the filtered list
- adapter.filterList(filterdNames)
- }
Advertisement
Advertisement