【问题标题】:How to filter with edit text with FirebaseRecyclerAdapter in android using kotlin如何使用 kotlin 在 android 中使用 FirebaseRecyclerAdapter 过滤编辑文本
【发布时间】:2021-06-03 02:53:32
【问题描述】:

请任何人帮助我使用带有 FireBaseRecyclerAdapter 的 editText 来过滤搜索。抱歉,我是 android 和 kotlin 的初学者。我有一个与 editText 和 recyclerview 关联的活动,并且在开始活动时,它显示所有对象。这里的问题是如何使用edit text来过滤搜索recyclerview里面的object?

【问题讨论】:

  • 我可以自己弄清楚,感谢您阅读我的问题,只需 5 到 10 行代码即可解决此问题。我将 youtube 链接放在这里,供与我有同样问题的任何人使用。 youtube.com/watch?v=Pr4Jz5Lc5pM&t=508s
  • 我认为article,可能会有所帮助。

标签: android firebase kotlin


【解决方案1】:

我在这里假设您的对象是“汽车” 创建一个按名称过滤汽车并返回汽车列表的方法。

public static List<Car> filterCars(List<Car> cars, String searchText) {
    List<Car> filteredCars = new ArrayList<>();
        Car c;
        for(int i = 0; i < cars.size(); i++) {
            c = cars.get(i);
            if (c.getCarName().toLowerCase().contains(searchText.trim().toLowerCase())) {
                filteredCars.add(c);
            }
        }
        return  filteredCars;
}

此函数接受汽车列表作为输入和一个字符串以在汽车名称中搜索,并返回与汽车名称匹配的汽车列表和 searchText。

现在,在活动中,在您键入搜索文本的 EditText 中,添加 TextchangeListener。

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //grab your list to display in recyclerview.
            // suppose it is carList
            List<Car> newList = filterCars(YourListOfCar, editable.toString().trim());
            
           // now this newList contains filtered car's list
           // now send it to the adapter and call adapter function
           // notifyDataSetChanged()
        }
    }
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多