Home > Mobile >  How can I solve 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

Time:11-24

I know there are a lot of questions about that error. Tried many solutions, I realized something was different in my case. Can anyone please help? I tried to apply searchview to recyclerview, and inserted filter() in reyclerview adapter and setOnQueryTextListener in mainactivity.

But I got the following error: 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>

I don't know what is my fault. How can I solve this?

CodePudding user response:

In your imports,

Replace

import android.widget.SearchView

With

import androidx.appcompat.widget.SearchView

CodePudding user response:

In xml, you use androidx SearchView

 androidx.appcompat.widget.SearchView

but in your kotlin code you android.widget.SearchView

import android.widget.SearchView

That's why can't cast

  • Related