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

RecyclerView集成SearchView后无法展示搜索结果问题求助

问题根因

筛选逻辑和列表渲染的数据源不一致:适配器的onBindViewHolder、getItemCount读取的是全量原始数据源myOfficerslistfull,但筛选后仅更新了用于展示的列表myOfficerslist,渲染层没有读取更新后的列表,所以看不到筛选结果。

修复步骤

1. 修正适配器渲染逻辑

将onBindViewHolder和getItemCount中所有读取myOfficerslistfull的代码,替换为读取当前展示用的列表myOfficerslist:

@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
    holder.textName.setText(myOfficerslist.get(position).getName());
    holder.textDesignation.setText(myOfficerslist.get(position).getDesignation());
    holder.textPosting.setText(myOfficerslist.get(position).getPlaceofposting());
    holder.textPhone.setText(myOfficerslist.get(position).getCell());
    holder.textEmail.setText(myOfficerslist.get(position).getEmail());
    holder.profileimage.setImageResource(myOfficerslist.get(position).getProfileImage());
}

@Override
public int getItemCount() {
    return myOfficerslist.size();
}

两个列表的作用要明确区分:

  • myOfficerslistfull:只读的全量原始数据,仅作为过滤的基准数据源,不要修改
  • myOfficerslist:存储当前需要展示的列表数据,筛选后更新的是该列表,渲染层仅读取该列表

2. 修正Activity菜单返回值

将onCreateOptionsMenu的返回值从super.onCreateOptionsMenu(menu)改为true,确保菜单事件正常生效:

// 原代码
return super.onCreateOptionsMenu(menu);
// 修改后
return true;

3. 可选优化

删除performFiltering中冗余的长度判断,避免异常:

// 原代码
if (searchText.length()==00 || searchText.isEmpty())
// 修改后
if (searchText.isEmpty())

内容的提问来源于stack exchange,提问作者Md Mobarak Hossen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 13:18:01