searchview 不适用于自定义适配器 kotlin

searchview is not working with custome adapter kotlin

这是我的 serachview 和自定义适配器代码。未调用自定义适配器内的函数。没有错误,但它不起作用,当我尝试在我的列表中搜索时没有任何反应:

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextChange(newText: String): Boolean {
                if (TextUtils.isEmpty(newText)) {
                    // adapter?.filter
                    adapter?.filter("")
                    listView.clearTextFilter();
                } else {
                    Log.i("searched ", newText)
                    adapter?.filter(newText);
                }
                return true;
            }

            override fun onQueryTextSubmit(query: String): Boolean {
                // task HERE
                return false
            }

        })

我的自定义适配器内部 class 片段内 :

   inner class CustomAdapter(
        private val context: Context,
        private val ItemDataList: List<ContactEntity>
    ) : BaseAdapter() {

        private val inflater: LayoutInflater =
            this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        override fun getCount(): Int {
            return ItemDataList.size
        }

        override fun getItem(position: Int): Int {
            return position
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }


        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var contact = ItemDataList[position]

            
            val rowView = inflater.inflate(R.layout.list_item_contact, parent, false)
            rowView.findViewById<TextView>(R.id.customer_name).text = contact.name
            rowView.tag = position
            return rowView
        }


        // Filter Class
        fun filterCustomer(charText: String) {
            Log.i("searched inside:", charText)
            var charText = charText
            charText = charText.toLowerCase(Locale.getDefault())
            mContacts = null
            if (charText.isEmpty()) {
               mContacts = ItemDataList
            } else {
                for (contact in ItemDataList) {
                    if (contact.name?.toLowerCase(Locale.getDefault())?.contains(charText)!!) {
                        mContacts = listOf(contact)
                    }
                }
            }
            notifyDataSetChanged()
        }

       

        
    }

这是我的 xml,我在列表视图上方添加了搜索视图:

 <SearchView
            android:id="@+id/searchView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <ListView
            android:id="@+id/contact_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

我的代码无法正常工作,因为我没有正确访问自定义适配器的方法。适配器实例为空。

我改变了这个:

 adapter?.filter(newText);

至:

 (listView.adapter as CustomAdapter?)?.filter(newText)

成功了!