使用Firebase RecyclerView的Android搜索视图无法显示数据

时间:2017-09-12 11:41:57

标签: android firebase firebase-realtime-database android-recyclerview searchview

我可以通过RecyclerView从Firebase检索数据到App。但是,当我尝试添加搜索视图时,它无法正常工作。

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

   getMenuInflater().inflate(R.menu.menu_search, menu);
    final SearchView searchView =(SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menusearch));
    SearchManager searchManager =(SearchManager) getSystemService(SEARCH_SERVICE);        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName());
    //searchView.setSubmitButtonEnabled(true);

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

       @Override
       public boolean onQueryTextChange(String newText) {
          //SearchText.setText(newText);
           Query Q = mDatabase.child("Recipees").orderByChild("cusine").startAt(newText).endAt("");
           FirebaseRecyclerAdapter<ImageRetrieve, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ImageRetrieve, BlogViewHolder>(

                   ImageRetrieve.class,
                   R.layout.recipe_row,
                   BlogViewHolder.class,
                   Q

           ) {
               @Override
               protected void populateViewHolder(BlogViewHolder viewHolder, ImageRetrieve model, int position) {

                   viewHolder.setCusine(model.getCusine());
                   viewHolder.setCatagory(model.getCatagory());
                   viewHolder.setRecipe(model.getRecipe());
                   viewHolder.setUrl(getApplicationContext(), model.getUrl());

               }
           };

           mRecipeList.setAdapter(firebaseRecyclerAdapter);



           return false;
       }


   });
   return true;
   }

3 个答案:

答案 0 :(得分:1)

试试这可能有所帮助:

Query Q = mDatabase.child("Recipees").orderByChild("cusine").startAt(newText).endAt(newText+"\uf8ff");

答案 1 :(得分:0)

所以这意味着查询有错误。 试试这个: - &gt;

mDatabase.child("Recipees").orderByChild("cusine").equalTo(n‌​ewText);

答案 2 :(得分:0)

尝试以下代码,可能会有所帮助:

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

        @Override
        public boolean onQueryTextChange(String text) {

            // if searchView is not empty
            if(!text.isEmpty()){
              // call seearch method if user starts typing
                search(text);
            }

            //else
            else{
                // set TextField empty
                search("");
            }

            return true;
        }
    });

//执行搜索的方法

private void search(String newText)
{

   ArrayList<> searchList = new ArrayList<>();


    // creates a query to initiate the search
   Query Q = mDatabase.child("Recipees")
    .orderByChild("cusine").startAt(newText).endAt(newText + "\uf8ff");


    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if(dataSnapshot.hasChildren()){

                //clears the arrayList
                searchList.clear();

                for(DataSnapshot snapshot : dataSnapshot.getChildren()){

                    final ImageRetrieve imageRetrieve = 
                     snapshot.getValue(ImageRetrieve.class);

                    //adds the rooms searched to the arrayList
                    searchList.add(imageRetrieve);

                }

            //NB: recyclerView is the view object in your activity that populates data

                FirebaseRecyclerAdapter firebaseRecyclerAdapter =
                        new FirebaseRecyclerAdapter(yourActivity.this,searchList);
                recyclerView.setAdapter(firebaseRecyclerAdapter);

                firebaseRecyclerAdapter.notifyDataSetChanged();

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // display message if error occurs

        }
    });

}