You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView集成SearchView无报错但搜索结果不展示问题求助

问题排查与修复方案

核心问题列表

  1. 列表渲染数据源不匹配:你在publishResults中把搜索结果存入了profileFilterList,但onBindViewHolder、getItemCount方法中依然使用原始的profiles列表作为渲染数据源,导致搜索结果永远不会展示到界面上。
  2. 无原始数据源备份:当前过滤逻辑直接操作当前展示的列表,多次搜索后会不断缩小搜索范围,无法回到完整数据;且ViewModel加载完数据调用updateProfile更新列表时,没有同步更新过滤用的数据源,导致搜索的是初始的空列表。
  3. 搜索维度不全:当前过滤逻辑仅匹配name和fatherName字段,缺少ID维度的匹配,不符合你的需求。
  4. 过滤逻辑缺少大小写兼容:默认的contains方法区分大小写,会导致用户大小写输入不匹配时搜索不到结果。

修复后的适配器代码

class ProfileListAdapter(private var originalProfiles:ArrayList<Profile>):RecyclerView.Adapter<ProfileListAdapter.ProfileViewHolder>(),Filterable {

    // 用于渲染的列表,默认等于原始全量数据
    private var showProfiles = ArrayList<Profile>()
    init {
        showProfiles.addAll(originalProfiles)
    }

    fun updateProfile(newProfiles:List<Profile>){
        originalProfiles.clear()
        originalProfiles.addAll(newProfiles)
        // 数据更新后默认展示全量数据
        showProfiles.clear()
        showProfiles.addAll(newProfiles)
        notifyDataSetChanged()
    }

    class ProfileViewHolder(view: View):RecyclerView.ViewHolder(view){
        private var id = view.findViewById<TextView>(R.id.tv_id)
        private var name = view.findViewById<TextView>(R.id.tv_name)
        private var fatherName = view.findViewById<TextView>(R.id.tv_father_name)
        private var profileImage = view.findViewById<ImageView>(R.id.iv_profile_image)
        private var progressDrawable = getProgressDrawable(view.context)

        fun bind(profile: Profile){
            id.text = profile.id.toString()
            name.text = profile.name
            // 若fatherName对应首都字段保留即可,可根据实际字段名调整
            fatherName.text = profile.fatherName
            profileImage.loadImage(profile.profilePicture,progressDrawable)
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ProfileViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.rv_dummy_items,parent,false)
    )
    override fun onBindViewHolder(holder: ProfileViewHolder, position: Int) {
        holder.bind(showProfiles[position])
    }
    override fun getItemCount() = showProfiles.size


    // 搜索逻辑
    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(constraint: CharSequence?): FilterResults {
                val keyword = constraint?.toString()?.trim() ?: ""
                val filteredList = if (keyword.isEmpty()){
                    // 搜索词为空返回全量原始数据
                    originalProfiles
                } else {
                    originalProfiles.filter {
                        // 同时匹配ID、国家名称、首都三个维度,忽略大小写
                        it.id.toString().contains(keyword, ignoreCase = true)
                                || it.name.contains(keyword, ignoreCase = true)
                                || it.fatherName.contains(keyword, ignoreCase = true)
                    } as ArrayList<Profile>
                }
                return FilterResults().apply { values = filteredList }
            }
            override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
                results?.values?.let {
                    showProfiles = it as ArrayList<Profile>
                    notifyDataSetChanged()
                }
            }
        }
    }
}

验证说明

主Activity代码无需修改,修复后运行项目即可看到搜索功能正常生效,三个维度的搜索都能正常命中,清空搜索词后自动恢复全量数据展示。

内容的提问来源于stack exchange,提问作者faiz dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 10:57:03