当前位置: 首页 > 知识库问答 >
问题:

Android-将搜索过滤器实现到RecyclerView

乜明朗
2023-03-14
public class DisplayAdapter extends RecyclerView.Adapter<DisplayAdapter.MyViewHolder>{
    private List<DataHolder> displayedList;
    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView english_d;
        public MyViewHolder(View view){
              super(view);
              english_d = (TextView) view.findViewById(R.id.engword);
        }
   }
    public DisplayAdapter(List<DataHolder> displayedList){
        this.displayedList = displayedList;
      }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position)    {
    // create a layout
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

    return new  MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    DataHolder content = displayedList.get(position);
    holder.english_d.setText(content.getEnglish());
   }
@Override
public int getItemCount() {
    return displayedList.size();
   }
}
public class DataHolder {
private String english;


public DataHolder() {
}

public DataHolder(String english, String german) {
    this.english = english;
   
}

public String getEnglish() {
    return english;
}

public void setEnglish(String english) {
    this.english = english;
   }

}

下面是我正在研究的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_two_fragment, container, false);

        rv = (RecyclerView) view.findViewById(R.id.list_view_english);
        rv.setHasFixedSize(true);
       
        inputSearch = (EditText) view.findViewById(R.id.inputSearch);
        
        rv.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(layoutManager);

        
        DisplayContent();
        

        disp_adapter = new DisplayAdapter(displayedList);
        
        rv.setAdapter(disp_adapter);
   return view;
}

共有1个答案

许典
2023-03-14

在适配器中添加更新列表的新函数

public void updateList(List<DataHolder> list){
     displayedList = list;
     notifyDataSetChanged();
}

add textWatcher for search允许您使用Edittext作为搜索字段

searchField.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // filter your list from your input
            filter(s.toString());
            //you can use runnable postDelayed like 500 ms to delay search text
        }
    });

简单过滤函数

void filter(String text){
     List<DataHolder> temp = new ArrayList();
     for(DataHolder d: displayedList){
           //or use .equal(text) with you want equal match
           //use .toLowerCase() for better matches
           if(d.getEnglish().contains(text)){
               temp.add(d);
           }
     }
     //update recyclerview
     disp_adapter.updateList(temp);
}
@OnTextChanged(R.id.feature_manager_search)
protected void onTextChanged(CharSequence text) {
    filter(text.toString());
}
 类似资料:
  • 问题内容: 我正在尝试搜索数据库,并能够使用大写/小写过滤器术语,但是我注意到在应用分析器时,我不知道如何在过滤后的搜索中应用小写分析器。这是查询: 我有一个小写的类型: 以及相应的映射: 问题答案: 问题是您在索引期间分析了一个字段以使其小写,但您正在对未分析的查询使用术语过滤器: 术语过滤器 筛选具有包含术语(未分析)的字段的文档。与术语查询类似,不同之处在于它充当过滤器。 http://ww

  • 问题内容: 我是这个框架的新手,因此练习Angularjs并遵循网站上提供的教程。 有一个示例,我们可以搜索表中存在的数据,示例如下, 在上面的代码中,我可以使用两个不同的输入来搜索手机,即按型号名称搜索和按公司名称搜索,以上代码运行良好, 但是,如果我需要使用选择选项中存在的搜索类型进行搜索,该怎么办? 代码如下 从上面的代码中,您可以看到我正在尝试通过选择框中显示的“姓名”,“公司”或“名称”

  • 本文向大家介绍Android RecyclerView添加搜索过滤器的示例代码,包括了Android RecyclerView添加搜索过滤器的示例代码的使用技巧和注意事项,需要的朋友参考一下 搜索过滤功能,相信大家都能用到,一般都是针对列表进行过滤的。下面给大家提供一种过滤列表的方法。 老规矩,先上图 RecycleView搜索过滤器-getFilter() Android 提供了Filetera

  • 我只是想在youtube上搜索我自己的视频,在X日期后发布 然而,当我使用< code>publishAfter参数时,即使我将< code>type参数设置为< code>video,也会出现< code>invalidSearchFilter错误。 错误描述如下: 该请求包含无效的搜索过滤器和/或限制组合。请注意,如果您将或参数设置为,则必须将参数设置为,如果您为、、、、、、、、或参数设置值,

  • 我有个问题。在常见的购物车网站有功能搜索产品与多个过滤器。例如,我正在搜索运动装备与一些过滤器,如: > 制造商 [x]耐克 阿迪达斯 茵宝 选项 尺寸 [x]s [x]M L [x]白色 黄色 红色 [x]蓝色 这是我的地图 部分测试数据 有了这个查询,一切都很好 但是,如果我在选项过滤器中添加更多的条件,我没有得到结果 我不知道我在映射或查询中是否错了,或者您能告诉我在这种情况下创建映射的最佳

  • 每次在searchview中搜索时,我都在努力阻止索引越界异常崩溃。我尝试了网上列出的几件事:搜索时,从索引绑定异常中获取数组 https://www.codeproject.com/Questions/1040119/Android-app-is-crashing-with-Index-out-of-range-is 但到目前为止,一切都无济于事。这个问题是我的代码特有的,只是我不知道问题出在哪