当选择出现在搜索视图中的建议时,从SearchRecentSuggestionsProvider检索查询:Android

时间:2018-06-25 11:50:47

标签: android kotlin searchview

我正在尝试按照https://developer.android.com/guide/topics/search/adding-recent-query-suggestions教程在应用中实现搜索视图。我可以进行最近的搜索,也可以从最近的搜索列表中获得所选项目的位置。但是找不到在该位置获得最近搜索价值的方法。

在这里,我的可搜索项和主要活动是相同的,并且我不想更改本教程中所示的片段。我想在同一活动中显示结果。我想从点击的建议中获得价值。

这是我的代码。

MySuggestionProvider

class MySuggestionProvider : SearchRecentSuggestionsProvider() {
    init {
        setupSuggestions(AUTHORITY, MODE)
    }

    override fun query(uri: Uri?, projection: Array<out String>?, queryArgs: Bundle?, cancellationSignal: CancellationSignal?): Cursor {
        return super.query(uri, null, queryArgs, cancellationSignal)
    }

    override fun query(uri: Uri?, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor {
        return super.query(uri, null, selection, selectionArgs, null)
    }

    override fun query(uri: Uri?, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?, cancellationSignal: CancellationSignal?): Cursor {
        return super.query(uri, null, selection, selectionArgs, null, cancellationSignal)
    }

    companion object {
        val AUTHORITY = "com.idea_implement_india.bloombench_and_kossine.iotcontrollerapp.AssociateClasses.SearchViewClasses.MySuggestionProvider"
        val MODE = DATABASE_MODE_QUERIES
    }
}

MainActivity

class MainActivity : AppCompatActivity(),
        NavigationView.OnNavigationItemSelectedListener,
        SearchView.OnQueryTextListener,
        SearchView.OnSuggestionListener  {


    private var suggestions : SearchRecentSuggestions? = null
    private var searchView : SearchView? = null

    override fun onQueryTextChange(newText: String?): Boolean {
        Log.e(TAG,"onQueryTextChange")
        return true
    }

    override fun onQueryTextSubmit(query: String?): Boolean {
        suggestions?.saveRecentQuery(query,null)
        Log.e(TAG,"onQueryTextSubmit")
        return true
    }

    override fun onSuggestionClick(position: Int): Boolean {
        Log.e(TAG,"onSuggestionClick $position")

        return true
    }

    override fun onSuggestionSelect(position: Int): Boolean {
        Log.e(TAG,"onSuggestionSelect $position")
        return true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        layoutWidth = coordinatorLayoutSuper.width

        val toggle = ActionBarDrawerToggle(this, drawer_layout,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)

        getToolbarNavigationIcon(toolbar)?.setOnClickListener({
            searchView?.isIconified = true
            drawer_layout.openDrawer(Gravity.START)
            Log.e(TAG,"Found Button")
        })
        ifKeyboarUp()

        rootLayout = findViewById(R.id.coordinatorLayoutSuper)

        val transaction = supportFragmentManager.beginTransaction()
        fragment = supportFragmentManager.findFragmentById(R.id.frameInMainActivity)

        if (fragment == null){
            fragment = ChooseMainActivity()
            transaction.add(R.id.frameInMainActivity,fragment).commit()
        }


    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.main_menu, menu)
        val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
        searchView = menu.findItem(R.id.menu_search).actionView as SearchView?

        searchView?.setSearchableInfo(searchManager.getSearchableInfo(componentName))

        suggestions = SearchRecentSuggestions(this@MainActivity,
                MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE)
        if (searchView == null || suggestions == null) {
            return true
        }

        searchView!!.setSearchableInfo(searchManager.getSearchableInfo(componentName))
        searchView!!.setOnQueryTextListener(this)
        searchView!!.setOnSuggestionListener(this)
        return true
    }

}

我在StackOverflow上看到了一些结果,并搜索了android文档,但似乎到处都在使用单独的搜索活动。我不想为此再创建一个活动。如果可以在 onSuggestionClick 中获取字符串,那就太好了。 预先谢谢你。

0 个答案:

没有答案