如何使用SearchView工具栏过滤数据库Firebase

时间:2017-09-30 11:30:44

标签: android firebase-realtime-database

我想实现一个searchview来根据用户输入来过滤来自firebase数据库的数据,但是当我尝试这样做时,结果并没有显示没有数据更新。

我试过寻找一些答案然后尝试了但是还没有成功

这是我的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_resep, container, false);

    initFirebase();
    initRecylerView(view);
    setData();
    return view;

}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem mSearchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) mSearchMenuItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Toast.makeText(context,query,Toast.LENGTH_LONG).show();
            searcDataFirebase(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            return false;
        }
    });
}

private void initFirebase(){
    database = FirebaseDatabase.getInstance();
    reference = database.getReference().child("jamu");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {

                //instance object to get data
                model = postSnapshot.getValue(ModelResep.class);
//                    Log.e("DATA ",model.getBahan()+model.getNama()+model.getKhasiat()+model.getLinkGambar()+model.getPembuatan());

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void searcDataFirebase(final String search){
    database = FirebaseDatabase.getInstance();
    reference = database.getReference();

    Query query = reference.child("jamu").orderByChild("NamaJamu").equalTo(search);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {

                //instance object to get data
                 model = postSnapshot.getValue(ModelResep.class);
//                    Log.e("DATA ",model.getBahan()+model.getNamaJamu()+model.getKhasiat()+model.getLinkGambar()+model.getPembuatan());

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private void initRecylerView(View view){
    recyclerView = (RecyclerView)view.findViewById(R.id.rv_resep);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    layoutManager.setReverseLayout(true);
    layoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(layoutManager);
}

private void setData(){
    //instace object from lib firebase ui and set data
    adapter = new FirebaseRecyclerAdapter<ModelResep, AdapterResep>(

            ModelResep.class,
            R.layout.rows_jamu,
            AdapterResep.class,
            reference
    ) {
        @Override
        protected void populateViewHolder(AdapterResep viewHolder, final ModelResep model, int position) {

            //get id from click position
            final String id = getRef(position).getKey();

            //set data adapter from model
            viewHolder.setNama(model.getNamaJamu());
            viewHolder.setImage(context,model.getLinkGambar());

            //click listener for update data
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,DetailResepActivity.class);
                    intent.putExtra("id",id);
                    intent.putExtra("NamaJamu",model.getNamaJamu());
                    intent.putExtra("Bahan",model.getBahan());
                    intent.putExtra("Khasiat",model.getKhasiat());
                    intent.putExtra("LinkGambar",model.getLinkGambar());
                    intent.putExtra("Pembuatan",model.getPembuatan());
                    startActivity(intent);
                }
            });

        }
    };

    //set data adapter
    recyclerView.setAdapter(adapter);

}

1 个答案:

答案 0 :(得分:1)

您不需要在任何阵列中创建新方法&#34; firebaseUserSearch&#34; 复制您的RecyclerView适配器并粘贴到里面。使用databaseReference来过滤它。

我的FB结构是:

 BLABLA:
     Bars:
         01: 
            BarName: "Best bar"
         02:
            BarName: "The Worst bar"

代码在这里:

  ....

    databaseReference = FirebaseDatabase.getInstance().getReference ("Bars");

  .....


  searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String searchText) {

            // TODO Auto-generated method stub

            return false;
        }

        @Override
        public boolean onQueryTextChange(String searchText) {

            searchText = searchView.getQuery().toString();

            firebaseUserSearch (searchText);

            return true;
        }
    });
    return true;
}



private void firebaseUserSearch (final String searchText) {


    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <Bar, ViewHolder>(

            Bar.class,
            R.layout.custom_row,
            ViewHolder.class,
       databaseReference.orderByChild("barName").startAt(searchText.toUpperCase()).endAt(searchText + "\uf8ff"))
    {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, Bar model, final int position) {

  ....