我如何解决 androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
How can I solve androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
我知道有很多关于该错误的问题。尝试了很多解决方案,我意识到我的情况有所不同。有人可以帮忙吗?
我尝试将 searchview
应用于 recyclerview
,并在 reyclerview adapter
中插入 filter()
并在 mainactivity
中插入 setOnQueryTextListener
。
但是我得到了以下错误:
androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
MainActivity.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
这就是为什么不能施法
我知道有很多关于该错误的问题。尝试了很多解决方案,我意识到我的情况有所不同。有人可以帮忙吗?
我尝试将 searchview
应用于 recyclerview
,并在 reyclerview adapter
中插入 filter()
并在 mainactivity
中插入 setOnQueryTextListener
。
但是我得到了以下错误:
androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
MainActivity.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
这就是为什么不能施法