无法在SearchView中实现筛选器。 (SearchView + FirebaseRecyclerAdapter) [英] not able to implement the filter in SearchView. (SearchView+ FirebaseRecyclerAdapter)

查看:291
本文介绍了无法在SearchView中实现筛选器。 (SearchView + FirebaseRecyclerAdapter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase-UI的FirebaseRecyclerAdapter。我无法在SearchView中实现过滤器。



我的数据库

阅读本文您将了解Firebase实时数据库搜索的原因以及如何实施搜索查询。

$ b

您需要更新您的代码的一行。
$ b $ pre $ Q $ c查询Q = mAllUsersDatabaseRef.child(User) .orderByChild( 用户名)startAt(newText).endAt(newText + \\\);

这只适用于从头开始的匹配。您还可以了解有关 startAt()的详细信息, endAt() equalTo() limitToFirst() limitToLast(),它们将帮助您更好地进行搜索查询,而无需使用除firebase之外的任何外部资源。



我也希望 Firebase实时数据库规则


I am using the FirebaseRecyclerAdapter of Firebase-UI. I'm not able to implement the filter in SearchView.

My database:

My Question:

How to search in FirebaseDatabase by character using FirebaseRecyclerAdapter ?
(I want to search by username)

My Code: I used this method for search, what's wrong in my code?

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

        @Override
        public boolean onQueryTextChange(String newText) {

            Query Q = mAllUsersDatabaseRef.child("User").orderByChild("username").startAt(newText).endAt("~");

            FirebaseRecyclerAdapter<User, SearchFeedFragment.AllRegisterUserViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, SearchFeedFragment.AllRegisterUserViewHolder>(
                    User.class, R.layout.row_search_all_users, SearchFeedFragment.AllRegisterUserViewHolder.class, Q) {
                @Override
                protected void populateViewHolder( SearchFeedFragment.AllRegisterUserViewHolder viewHolder,User model, int position) {


                    viewHolder.setUserName(model.getUsername());
                    viewHolder.setUserProfilePic(Common.stringToBitMap(model.getProfilepicurl()));

                }
            };
            mRecyclerViewSearch.setAdapter(firebaseRecyclerAdapter);
            return false;
        }
    });


public static class AllRegisterUserViewHolder extends RecyclerView.ViewHolder {

        View mView;

        ImageView mImgProfileImage = (ImageView) itemView.findViewById(R.id.iv_searchProfilePic);
        TextView mTxtProfileName = (TextView) itemView.findViewById(R.id.txt_searchProfileName);

        public AllRegisterUserViewHolder(View itemView) {
            super(itemView);
            itemView = mView;
        }

        public void setUserName(String username) {
            mTxtProfileName.setText(username);
        }

        public void setUserProfilePic(Bitmap image) {
            mImgProfileImage.setImageBitmap(image);
        }
}

解决方案

Implementing Perfect Search with Firebase Realtime Database

Queries, Part 2: Advanced Searches with Firebase, made Plug-and-Play Simple

Read this and you will learn why and how about Firebase Realtime Database Search and how to implement a search Query.

An Alternative solution for your case

you need to update one line of your code.

Query Q = mAllUsersDatabaseRef.child("User").orderByChild("username").startAt(newText).endAt(newText+"\uf8ff");

this will work for matching from start only. Also you can learn more about startAt(),endAt(),equalTo(),limitToFirst() and limitToLast() they will help you make your search query better without using any external resource other than firebase.

also i hope there is no issues with the Firebase Realtime Database Rules.

这篇关于无法在SearchView中实现筛选器。 (SearchView + FirebaseRecyclerAdapter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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