从Firebase填充recyclerView的SearchView问题

时间:2019-06-03 12:18:13

标签: android firebase android-recyclerview

我有一个从Firebase数据库填充的RecyclerView。该列表很长,因此我希望用户能够搜索该列表。但是,当用户在SearchView中键入第一个字符时,将不会返回任何内容。

这是我的收藏夹片段中的onCreateView 已更新

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

    mAuth = FirebaseAuth.getInstance();
    current_user_id = mAuth.getCurrentUser().getUid();

    recyclerView = (RecyclerView) view.findViewById(R.id.route_favourites);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    search = (SearchView) view.findViewById(R.id.search);

    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("BusRoute");
            rootRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for(DataSnapshot datasnapshot : dataSnapshot.getChildren()){
                        String key = datasnapshot.getKey();

                        Query query = rootRef.child(key).orderByChild("route").equalTo(newText);
                        query.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                favouritesAdapter.notifyDataSetChanged();

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

            return true;
        }
    });

    favouritesAdapter = new FavouritesAdapter(getDataSetHistory(), getActivity());
    favouritesAdapter.notifyDataSetChanged();
    recyclerView.setAdapter(favouritesAdapter);

    getBusRoutes();
    return view;
}

这是我的最爱适配器:

public class FavouritesAdapter extends RecyclerView.Adapter<FavouritesAdapter.ViewHolder> implements Filterable {
    private List<BusRoute> favourites;
    private List<BusRoute> searchList;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView routeTextView, originTextView, destinationTextView;
        ImageView addFavouritesButton;

        ViewHolder(View v){
            super(v);
            this.routeTextView = v.findViewById(R.id.route);
            this.destinationTextView = v.findViewById(R.id.destination);
            this.originTextView = v.findViewById(R.id.origin);
            this.addFavouritesButton = v.findViewById(R.id.addToFavourites);
            addFavouritesButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String route = routeTextView.getText().toString();
                    System.out.println("clicked");
                    System.out.println(route);
                    //addToFavourites(route);
                }
            });
        }

    @NonNull
    @Override
    public FavouritesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
       View view = LayoutInflater.from(context).inflate(R.layout.favourite_card, parent, false);

       return new FavouritesAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FavouritesAdapter.ViewHolder viewHolder, int i) {
        BusRoute bus = favourites.get(i);
        viewHolder.originTextView.setText(String.valueOf(bus.getOrigin()));
        viewHolder.destinationTextView.setText(String.valueOf(bus.getDestination()));
        viewHolder.routeTextView.setText(String.valueOf(bus.getRoute()));
    }

    public FavouritesAdapter(ArrayList<BusRoute> favourites, Context context){
        this.context = context;
        this.favourites = favourites;
        searchList = new ArrayList<>(favourites);
    }

    @Override
    public int getItemCount() {
        System.out.println(favourites);
        return favourites.size();
    }

    @Override
    public Filter getFilter() {
        return favouriteFilter;
    }

    private Filter favouriteFilter = new Filter(){

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            System.out.println("filter results");
           List<BusRoute> filteredList = new ArrayList<>();
           if(constraint==null || constraint.length()==0){
               filteredList.addAll(searchList);
           }else{
               String filterPattern = constraint.toString().toLowerCase().trim();

               for(BusRoute br : searchList){
                   if(br.getRoute().toLowerCase().contains(filterPattern)){
                       filteredList.add(br);
                   }else{
                       System.out.println("nothing here");
                   }
               }
           }
            System.out.println("set filter results");
           FilterResults results = new FilterResults();
           results.values = filteredList;
           return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            favourites.clear();
            favourites.addAll((List)results.values);
            notifyDataSetChanged();
        }
    };
}

我只想拥有一个可过滤的搜索栏,但无法使其正常运行!我将不胜感激!

2 个答案:

答案 0 :(得分:0)

在发布结果方法中。不要清除您的阵列列表。这就是空数据的原因

答案 1 :(得分:0)

Try this   
@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String query = charSequence.toString();

            List<String> filtered = new ArrayList<>();

            if (query.isEmpty()) {
                filtered = items;
            } else {
                for (String movie : items) {
                    if (movie.toLowerCase().contains(query.toLowerCase())) {
                        filtered.add(movie);
                    }
                }
            }

            FilterResults results = new FilterResults();
            results.count = filtered.size();
            results.values = filtered;
            return results;
        }