当我开始在搜索栏中输入时,没有调用 onQueryTextChange

onQueryTextChange is not being called when I start typing in the search bar

我正在为此烦恼。我正在尝试使用搜索视图小部件搜索我的数据库。我遵循了多个示例,但对我没有任何帮助。我正在记录 onQueryTextChange 函数,看看它是否在搜索栏中输入后被调用,但没有成功。任何人都可以在这里看到问题。 如果有帮助,请使用 room 和 koin。

我的主要Activity:

    import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupActionBarWithNavController
import com.durbindevs.tradiedairy.R
import com.durbindevs.tradiedairy.databinding.ActivityMainBinding
import com.durbindevs.tradiedairy.ui.roster.JobMotor
import org.koin.androidx.viewmodel.ext.android.viewModel

class MainActivity : AppCompatActivity(), SearchView.OnQueryTextListener {

    private val motor: JobMotor by viewModel()

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)

        supportFragmentManager.findFragmentById(R.id.nav_host)?.findNavController()?.let { nav ->
            appBarConfiguration = AppBarConfiguration(nav.graph)
            setupActionBarWithNavController(nav, appBarConfiguration)
        }

    }

    override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
        R.id.about -> {
            startActivity(Intent(this, AboutActivity::class.java))
            true
        }
        else -> super.onOptionsItemSelected(item)
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.action_search, menu)
        Log.d("search", "inflate")

            val search = menu?.findItem(R.id.search)
            val searchView = search?.actionView as? SearchView
            searchView?.isSubmitButtonEnabled = true
        searchView?.setIconifiedByDefault(false)
            searchView?.setOnQueryTextListener(this)

        return true


    }

    override fun onQueryTextSubmit(query: String?): Boolean {
       if (query != null){
            Log.d("search", "searching")
            searchDatabase(query)
       }
        return true
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        if (newText!= null) {
            Log.d("search", "searching")
            searchDatabase(newText)
        }
        return true

    }
    private fun searchDatabase(query: String) {
        val searchQuery = "%$query%"
        Log.d("search", "searching")
        motor.searchForItems(searchQuery)
    }

    override fun onSupportNavigateUp() =
        NavigationUI.navigateUp(findNavController(R.id.nav_host), appBarConfiguration)

}

我的清单


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.durbindevs.tradiedairy">

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <application
        android:name=".JobApp"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.JobDairy">
        <activity android:name=".ui.AboutActivity" />
        <activity android:name=".ui.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
    </application>

</manifest>

我的res菜单文件


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context =".ui.MainActivity">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="@string/menu_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="ifRoom|withText"
        tools:targetApi="o"/>
</menu>

只需将菜单资源文件中搜索视图项的“actionViewClass”更改为“androidx.appcompat.widget.SearchView”(androidx 命名空间)。

像这样:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context =".ui.MainActivity">
<item
    android:id="@+id/search"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="@string/menu_search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="ifRoom|withText"
    tools:targetApi="o"/>