looking for some solutions? You are welcome.

SOLVED: Firestore search for a specific document using whereequalto returns nothing

Mr.patel:

I'm trying to implement search feature inside firestore collection where it should fetch and display all document containing my entered string into my firestore recycler view but however my recycler view is always empty and is not returning any items. Please take a look at my code: //This method will load data into recycler view

private void initializeData()
    {
        db = FirebaseFirestore.getInstance();
      //Query query = null;
        Query  query;
        if (txt!=null){
             query = db.collection("Items").whereEqualTo(txt, true);

        }else{
              query = db.collection("Items");
        s="Items";
        }
        FirestoreRecyclerOptions<FriendsResponse> response = new FirestoreRecyclerOptions.Builder<FriendsResponse>()
                .setQuery(query, FriendsResponse.class)
                .build();
        adapter = new FirestoreRecyclerAdapter<FriendsResponse, FriendsHolder>(response) {
            @Override
            public void onBindViewHolder(FriendsHolder holder, int position, FriendsResponse model) {
                String id = getSnapshots().getSnapshot(position).getId();
                holder.exname.setText(id);
                db.collection("Items").document(id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        DocumentSnapshot document = task.getResult();
                        String url= (String) document.getData().get("imageurl");
                        // Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
                        Glide.with(getContext()).load(url).into(holder.imgmenu);
                    }
                });  

            }

            @Override
            public FriendsHolder onCreateViewHolder(ViewGroup group, int i) {
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.menu_item, group, false);

                return new FriendsHolder(view);
            }

            @Override
            public void onError(FirebaseFirestoreException e) {
                Log.e("error", e.getMessage());
            }
        };

        adapter.notifyDataSetChanged();
        //int spaceInPixels = 200;
        DividerItemDecoration verticalDecoration = new DividerItemDecoration(recyclerView.getContext(),
                DividerItemDecoration.HORIZONTAL);
        Drawable verticalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.divider_vertical);
        verticalDecoration.setDrawable(verticalDivider);
        recyclerView.addItemDecoration(verticalDecoration);

        DividerItemDecoration horizontalDecoration = new DividerItemDecoration(recyclerView.getContext(),
                DividerItemDecoration.VERTICAL);
        Drawable horizontalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.divider_horizontal);
        horizontalDecoration.setDrawable(horizontalDivider);
        recyclerView.addItemDecoration(horizontalDecoration);
        recyclerView.setAdapter(adapter);
}

This is the code for my search bar:

public void search(){
        //setContentView(R.layout.view_feed_toolbar);
        searchBar = (MaterialSearchBar) getActivity().findViewById(R.id.searchBar);
        searchBar.setSpeechMode(false);
        searchBar.enableSearch();
        searchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
            @Override
            public void onSearchStateChanged(boolean enabled) {
                String s = enabled ? "enabled" : "disabled";
                Toasty.info(getContext(), "Search " + s, Toast.LENGTH_SHORT,true).show();
                if (s.equals("disabled")){
                    searchBar.setVisibility(View.INVISIBLE);
                    txt=null;
                    initializeData();
                }
            }

            @Override
            public void onSearchConfirmed(CharSequence text) {
            txt=text.toString();
            initializeData();
            }

            @Override
            public void onButtonClicked(int buttonCode) {

            }
        });
    }



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent