这事真让我抓狂。我试图搜索我的数据库与搜索视图小部件。我已经遵循了多个例子,但没有一个对我有用。我正在记录onQueryTextChange函数,看看它是否在搜索栏输入后被调用,但没有运气。有人看到问题了吗?如果有帮助,可以使用room和koin。
我的主要活动:
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. apppat .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"/>