Kotlin 过滤器在 RecyclerView 中搜索 EditText

问题描述 投票:0回答:2

在我使用 Kotlin 的 Android 应用程序 Api 28 中,我创建了一个接口“listOfCountry.xml”,一个用于搜索的编辑文本和一个包含所有国家/地区列表的回收器视图,如下代码所示:

           <EditText
                android:id="@+id/search"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:padding="5dp"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:fontFamily="@font/cairo"
                android:hint="Search..."
                android:imeOptions="actionSearch"
                android:maxLines="1"
                android:singleLine="true"
                android:textSize="14sp"/>

    <TextView
        android:id="@+id/cancelSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/cairo"
        android:layout_marginTop="15dp"
        android:layout_marginStart="20dp"
        android:text="@string/cancel_msg"
        android:textSize="14sp" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewGovernmentOrSectionList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/lyt_search" />

我想通过搜索过滤器从列表中获取一个国家,以下代码是我的适配器的描述:

class CountryItemAdapter(var countries: Array<AddressesData>, var mListener: OnItemClickListener) : RecyclerView.Adapter<CountryItemAdapter.ViewHolder>()
        , Filterable {

    private var selectedPos = -1
    var searchableList: MutableList<AddressesData> = arrayListOf()
    private var onNothingFound: (() -> Unit)? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_country, parent, false)
        return ViewHolder(view)
    }
    override fun getItemCount() = countries.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item: AddressesData = countries[position]
        holder.tickImage.visibility = (if (selectedPos == position) View.VISIBLE else View.GONE)
        holder.country.text = item.englishName.toString()

        holder.itemView.setOnClickListener {
            selectedPos = position
            mListener.onItemClick(holder.itemView, item)
            notifyDataSetChanged()
        }

    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val country: TextView = itemView.counrty
        val tickImage: ImageView = itemView.tickGovernment
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, viewModel: AddressesData)
    }
    override fun getFilter(): Filter {
        return object : Filter() {
            private val filterResults = FilterResults()
            override fun performFiltering(constraint: CharSequence?): FilterResults {
                searchableList.clear()
                if (constraint.isNullOrBlank()) {
                    searchableList.addAll(countries)
                } else {
                    val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' }
                    for (item in 0..countries.size) {
                        if (countries[item].englishName!!.toLowerCase().contains(filterPattern)) {
                            searchableList.add(countries[item])
                        }
                    }}
                    return filterResults.also {
                        it.values = searchableList
                    }
                }

                override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
                    if (searchableList.isNullOrEmpty())
                        onNothingFound?.invoke()
                    notifyDataSetChanged()

                }

我在我的活动中添加以下代码:

search.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                adapter.filter.filter(charSequence.toString())

            }

            override fun afterTextChanged(editable: Editable) {}
        })

过滤器没有工作,我想知道我的代码中的问题在哪里以及如何更正它以使过滤器工作?

android kotlin android-recyclerview
2个回答
0
投票

我认为您在适配器中丢失了

getItemCount
方法,它必须返回适配器中项目的实际大小。


0
投票

来晚了,但愿它能帮助别人,

您正在过滤

searchableList
列表中的数据,但您已将
countries
列表应用于适配器。 相应地改变它会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.