0

I create a editText filter search. I want the recyclerView will show when user typing. In my apps, when user typing the filter was show, but the list recyclerView show too. maybe this video can explain what i mean filterSearch

DimasGs
  • 36
  • 3
  • Does this answer your question? [How to filter a RecyclerView with a SearchView](https://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview) – Martin Marconcini Jul 24 '20 at 10:34

1 Answers1

0

Here is my code which works

ADAPTER

    class SearchAdapter(val context: Context, val item:List<Item>) : RecyclerView.Adapter<SearchAdapter.ChildHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChildHolder {
            val inflator = LayoutInflater.from(context).inflate(R.layout.search_container,parent,false)
            return ChildHolder(inflator)
        }
    
        override fun getItemCount(): Int {
           return item.size
        }
        override fun onBindViewHolder(holder: ChildHolder, position: Int) {
            val current = item[position]
            holder.item.name.setText(current.name)
        }
    
        class ChildHolder(view: View) : RecyclerView.ViewHolder(view){
            val item = view.country_container
        }
    }
    
    
    data class Item(val name:String)

MAINACTIVITY

class MainActivity : AppCompatActivity() {
    var item:ArrayList<Item> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        item.add(
            Item("russia")
        )
        item.add(
            Item("india")
        )
        item.add(
            Item("usa")
        )
        item.add(
            Item("china")
        )
        item.add(
            Item("uk")
        )
        item.add(
            Item("brazil")
        )
        item.add(
            Item("pakistan")
        )
        item.add(
            Item("sri lanka")
        )
        search_view.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                TODO("Not yet implemented")
            }
            override fun onQueryTextChange(newText: String?): Boolean {
                val filtered = item.filter { item ->
                  item.name.contains(newText.toString())
                }
                constructRecycleView(filtered)
                return true
            }
        })
    }

    private fun constructRecycleView(item:List<Item>){
        val adapter = SearchAdapter(this,item)
        recycle_view.adapter = adapter
        recycle_view.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
    }
}

constructRecycleView method always create's new adapter, you can also create methods where you can mutate the adapter item and call notifyDatasetChanged Method ,and please implement onQueryTextSubmit on your own if not your application gives Error

 kotlin.NotImplementedError: An operation is not implemented: Not yet implemented

OUTPUT

enter image description here

enter image description here

user9116815
  • 23
  • 1
  • 10