使用firebaserecycleradapter过滤recyclerview [英] Filtering a recyclerview with a firebaserecycleradapter

查看:453
本文介绍了使用firebaserecycleradapter过滤recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有FirebaseRecyclerAdapter的RecyclerView。当用户开始在SearchView中输入名称时,我想填充RecyclerView。

  public class SchoolsAdapter extends FirebaseRecyclerAdapter<学校,SchoolsAdapter.SchoolViewHolder> {

public Sc​​hoolsAdapter(查询参考){
super(School.class,R.layout.item_school,SchoolViewHolder.class,ref);

$ b @Override
public void populateViewHolder(SchoolViewHolder schoolViewHolder,School school,int position){
schoolViewHolder.name.setText(school.getName());
schoolViewHolder.address.setText(school.getAddress());


static class SchoolViewHolder extends RecyclerView.ViewHolder {

public TextView name;
公共TextView地址;

public Sc​​hoolViewHolder(查看itemView){
super(itemView);
name =(TextView)itemView.findViewById(R.id.school_item_tview_name);
address =(TextView)itemView.findViewById(R.id.school_item_tview_address);





我猜我需要将QueryTextListener添加到将更新适配器中的查询的searchview。这会打破FirebaseRecyclerAdapter?



或者我应该

  @Override 
public boolean onQueryTextChange(String newText){
mRecyclerView.setAdapter(new SchoolAdapter(ref.orderByChild(name).startAt(userQuery).endAt(userQuery +〜))
return false;
}

每当用户键入内容时



另外,文档讨论了对firebase查询进行排序和排序,但是并没有明确地说出进行字符串模式匹配的最佳方法。什么是字符串匹配的最佳方式,以便回收器视图显示所有结果搜索查询作为数据库记录的一个子字符串,也可能是那些距离编辑的距离。



也是一种忽略查询的方法吗?

解决方案

我刚刚完成了一些你正在寻找的东西,我不知道这是最优雅的解决方案,但我会扔掉了我的想法,如果你认为我的想法将帮助,我可以肯定提供一些例子。首先,当我扩展了基本的FirebaseAdapter我添加了一个新的过滤器名为mFullList,因为mItems的FirebaseAdapter将用于显示列表,我不想继续回到网络,当我不必。然后我重写我的子类中的所有方法,使用Firebase回调中的值更新mFullList,对它们进行排序,过滤,然后使用新列表调用super.X()。



$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ,比较器)
getFilter()。filter(filterString)

filterString是适配器并在调用getFilter()。filter()时更新。在执行过滤器的过程中,我通过mFullList进行循环,并进行比较:

$ $ p $ $ $ $ $ mFullList.get(pos).getName() .toLowerCase()包含(filterString.toLowerCase)。一旦fitlering完成,你有一个新的列表,FilterResults对象中传递给Filter.publishResults。 publishResults调用执行更新和通知的类中的方法。



$ p $ filterCompleted(List)
getItems()。清除
getItems()。addAll
notify

本质上,我没有希望FirebaseAdapater停止获取项目的完整列表,我只是希望用户请求过滤完整列表并正确处理其请求。此外,我没有看到指向添加网络请求的用户输入一个额外的字符。



使用此方法,您可以使用:


$ b $ pre $ adapter.getFilter()。filter(something)

根据您更新的字段过滤列表,并且
$ b

  adapter.getFilter() .filter()

重置完整列表(只要您的performFilter()处理它FireBase的新更新将根据用户的选择以及用户输入新值的方式进行筛选,而不需要重新制作Firebase网络请求。


I have a RecyclerView with a FirebaseRecyclerAdapter. I want to populate the RecyclerView with a list of names when the user starts typing into the SearchView.

public class SchoolsAdapter extends FirebaseRecyclerAdapter<School, SchoolsAdapter.SchoolViewHolder> {

    public SchoolsAdapter(Query ref) {
        super(School.class, R.layout.item_school, SchoolViewHolder.class, ref);
    }

    @Override
    public void populateViewHolder(SchoolViewHolder schoolViewHolder, School school, int position) {
        schoolViewHolder.name.setText(school.getName());
        schoolViewHolder.address.setText(school.getAddress());
    }

    static class SchoolViewHolder extends RecyclerView.ViewHolder {

        public TextView name;
        public TextView address;

        public SchoolViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.school_item_tview_name);
            address = (TextView) itemView.findViewById(R.id.school_item_tview_address);
        }
    }
}

I'm guessing I need to add a QueryTextListener to the searchview that would update the Query in the adapter. Would this break the FirebaseRecyclerAdapter?

Or should I

@Override
public boolean onQueryTextChange(String newText) {
    mRecyclerView.setAdapter(new SchoolAdapter(ref.orderByChild("name").startAt(userQuery).endAt(userQuery+"~")) 
    return false;
}

whenever the user types something?

Also the docs talk about ordering and sorting firebase queries but don't explicitly say the best way to do string pattern matching. What's the best way to do string matching so that the recycler view shows all results which have the search query as a substring of the database record, and possibly those that are 1 edit distance away as well.

Also a way to ignorecase on queries?

解决方案

I just finished doing something near to what you're looking for, I'm not sure it's the most elegant solution, but I'll throw out some ideas and if you think my idea will help I can definitely provide some examples.

Firstly, when I extended the base FirebaseAdapter I added a new filter named mFullList, since mItems of the FirebaseAdapter will be used for the display list, I don't want to keep going back to the network when I didn't have to. I then override all the methods in my child class to update mFullList with the values from the Firebase callbacks, sort them, filter them then call super.X() with the new list.

Quickly:

public reset(List)
   mFullList = List
   Collections.sort(mFullList, Comparator)
   getFilter().filter(filterString)

The filterString is a field within the Adapter and is updated during the call to getFilter().filter(). During the perform filter I then loop through the mFullList and do a compare of:

mFullList.get(pos).getName().toLowerCase().contains(filterString.toLowerCase);

Once fitlering is done you have a new list that is passed to Filter.publishResults in the FilterResults object. publishResults calls a method in the class that performs the update and notify.

filterCompleted(List)
   getItems().clear
   getItems().addAll
   notify

Essentially, I didn't want the FirebaseAdapater to stop getting the full list of items, I just wanted the users request to filter that full list and handle their request appropriately. Also, I didn't see a point to the added network requests based the user typing an extra character.

Using this method you can just use:

adapter.getFilter().filter("something")

to filter the list based on your updated field, and

adapter.getFilter().filter("")

to reset the full list (as long as your performFilter() handled it correctly. This way new updates from FireBase will be filtered based on the users selection, as well as when a user types in new values, won't require making new Firebase network requests.

这篇关于使用firebaserecycleradapter过滤recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆