SearchView建议适配器未在Android中弹出

时间:2019-05-07 08:10:28

标签: android kotlin searchview simplecursoradapter

我有一个像这样的搜索菜单项

<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>

我要对此进行夸大,然后像这样为SearchView设置侦听器

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
    }

然后,我试图在从服务器取回数据后使用这样的SimpleCursorAdapter来显示它。

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))

因此,正在发生的事情是,我能够展开SearchView并键入它,然后我还从服务器获取了数据,但是没有弹出搜索建议列表。它只是空白。 我想念什么?任何帮助,将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

您可能需要设置Threshold

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

如果将“阈值”设置为3,这意味着它将在输入第一个字符后开始提供建议