searchview无法与定制适配器kotlin一起使用

时间:2020-08-27 13:32:55

标签: android listview kotlin filter searchview

这是我的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
            }

        })

我的自定义适配器内部类在片段中:

   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:

 <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"
            />

1 个答案:

答案 0 :(得分:0)

由于我的代码不起作用,我没有正确访问自定义适配器的方法。适配器的实例为空。

我更改了此内容

 adapter?.filter(newText);

对此:

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

成功了!