0

I am working on a project where I have a skills.txt file. Now I have to make a SearchView to search skills by input in real-time. I don't know how we do this. So I searched for it and find some ways like this.

But I'm not sure how to use it with a search view to get the desired result like this [source: LinkedIn]

enter image description here

mr. groot
  • 334
  • 4
  • 18

1 Answers1

0

In Your Activity onCreationOptionsMenu write this.

override fun onCreateOptionsMenu(menu: Menu): Boolean {

    menuInflater.inflate(R.menu.menu, menu)
    val search = menu.findItem(R.id.search)
    val searchView = search.actionView as androidx.appcompat.widget.SearchView
    searchView.setOnQueryTextListener(object : 
    androidx.appcompat.widget.SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            if (query != null && query.isNotEmpty()) {
                viewModel.searchResult(query)
            }
            return false
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            return true
        }
    })
    return super.onCreateOptionsMenu(menu)}

In your view Model write this. Once

 private val _searchResultData = SafeMediatorLiveData(initialValue = SearchResultResponse(emptyList()))

fun searchResult(skillSetToSearch: String) {
     // val arrayList = callFiletoSearchString(skillSetToSearch)
    _searchResultData.update(searchListResponse = arrayList)

 }

SafeMediatorLive Data class will look like this

class SafeMediatorLiveData<T : Any>(initialValue: T) : MediatorLiveData<T>() {

    init {
        value = initialValue
    }

    override fun getValue(): T = super.getValue()!!
}
Chiradeep
  • 973
  • 12
  • 32