简体   繁体   中英

SearchView suggestions adapter is not popping up in Android

I have a search menu item like this

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
            android:id="@+id/action_search"
            android:icon="@drawable/search_icon"
            android:title="Search"
            app:actionViewClass="android.widget.SearchView"
            app:showAsAction="always|collapseActionView"/>

</menu>

I am inflating this and then setting up listeners for SearchView like this

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.search_menu_item, menu)
        return true
    }

    override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        val mSearchMenuItem = menu?.findItem(R.id.action_search)


        mSearchMenuItem?.setOnActionExpandListener(object : MenuItem.OnActionExpandListener{
            override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
                return true
            }

            override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
                return true
            }

        })

        searchView = mSearchMenuItem?.actionView as SearchView

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                samSearchViewModel.searchData(query)

                return false
            }

            override fun onQueryTextChange(newText: String): Boolean {
                if (newText.length >= 3) {
                    samSearchViewModel.searchData(newText)
                }
                return false
            }
        })

        searchView.setOnCloseListener(object : SearchView.OnCloseListener {
            override fun onClose(): Boolean {
                return false
            }
        })  

        return true
    }

Then I am trying to display it using a SimpleCursorAdapter like this after getting data back from the server.

val strArr: Array<String?> = arrayOfNulls(t.datas?.size!!)
                        for(i in t.datas!!.indices){
                            strArr[i] = t.datas!![i]?.symbol
                        }

                        val searchManager = getSystemService(SEARCH_SERVICE) as SearchManager
                        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()))
                        val to = intArrayOf(android.R.id.text1)
                        searchView.setIconifiedByDefault(false)
                        searchView.setSuggestionsAdapter(SimpleCursorAdapter(this@MainActivity,android.R.layout.simple_list_item_1,null,strArr,to,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER))

So what's happening is, I am able to expand the SearchView, type in it and then I am also getting data back from the server but then the search suggestions list is not popping up. It is just blank. What am I missing? Any help would be appreciated. Thanks

You may need to set Threshold .

AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoCompleteTextView.setThreshold(3);

If you set Threshold to 3 that means it will start giving suggestions after typing first character

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM