如何解决androidx.appcompat.widget.SearchView无法转换为android.widget.



我知道关于这个错误有很多问题。尝试了许多解决方案,我意识到我的情况有所不同。有人能帮忙吗?我尝试将searchview应用于recyclerview,并在reyclerview adapter中插入filter(),在mainactivity中插入setOnQueryTextListener

但我得到了以下错误:androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView

主要活动.kt

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.co_investorux_ui.databinding.ActivityMainBinding
import com.example.co_investorux_ui.databinding.ItemViewBinding
import com.sothree.slidinguppanel.SlidingUpPanelLayout
import java.util.*
import kotlin.collections.ArrayList
import android.app.SearchManager
import android.widget.SearchView
import android.widget.SearchView.OnQueryTextListener
...
val searchview :SearchView = findViewById(R.id.search_view)
searchview.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter.filter.filter(newText)
return false
}
})
...

getFilter((

override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(charSequence: CharSequence): FilterResults {
val charString = charSequence.toString()
if (charString.isEmpty()) {
searchList = itemList
} else {
val filteredList = ArrayList<CoinList>()
for (coin in itemList) {
if (coin.name.contains(charString)) {
filteredList.add(coin)
}
}
searchList = filteredList
}
val filterResults = FilterResults()
filterResults.values = searchList
return filterResults
}
override fun publishResults(
charSequence: CharSequence,
filterResults: FilterResults
) {
searchList = filterResults.values as ArrayList<CoinList>
notifyDataSetChanged()
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout 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"
android:id="@+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
app:umanoDragView="@id/slide_layout"
app:umanoPanelHeight="16dp"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/first_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/recyclerview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/inside_layout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

我不知道是我的错。我该如何解决这个问题?

在您的进口中,

更换

import android.widget.SearchView

import androidx.appcompat.widget.SearchView

在xml中,您使用androidx SearchView

androidx.appcompat.widget.SearchView

但在你的kotlin代码中,你android.widget.SearchView

import android.widget.SearchView

这就是为什么不能铸造

相关内容

  • 没有找到相关文章

最新更新