user10831678
user10831678

Reputation:

Firebase search feature

Code

        mRecyclerView = (RecyclerView) rootview.findViewById(R.id.recyclerView);
    //Add the data first
    linearLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(linearLayoutManager);


    mDataabseReference = FirebaseDatabase.getInstance().getReference().child("UserData");


    mSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String mSearchText = mEditText.getText().toString();

            mSearch(mSearchText);
        }
    });
    return rootview;
}

private void mSearch(final String mSearchText) {
    Query searchQuery = mDataabseReference;

    FirebaseRecyclerOptions<SearchHelper> options =
            new FirebaseRecyclerOptions.Builder<SearchHelper>()
                    .setQuery(searchQuery, SearchHelper.class)
                    .build();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<SearchHelper, Search.SearchViewHolder>(options) {

        @Override
        protected void onBindViewHolder(@NonNull final Search.SearchViewHolder holder, int position, @NonNull final SearchHelper model) {


            String uid = getRef(position).getKey();
          mDataabseReference.child(uid).startAt(mSearchText).endAt(mSearchText + "\uf8ff").addValueEventListener(new ValueEventListener() {
              @Override
             public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                 for (DataSnapshot ds : dataSnapshot.getChildren()) {
                      SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
                      mSearchList.add(search);

                  }
              }

              @Override
              public void onChildChanged(DataSnapshot dataSnapshot, String s) {

              }
              @Override
              public void onChildRemoved(DataSnapshot dataSnapshot) {

              }

              @Override
              public void onChildMoved(DataSnapshot dataSnapshot, String s) {

              }

              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
                  mSearchList.add(search);
              }

              @Override
              public void onCancelled(DatabaseError databaseError) {

              }
          });
        }
        @Override
        public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }


    };
    mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();

}

public static class SearchViewHolder extends RecyclerView.ViewHolder {
        View mView;

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

        public void setName(String title) {
            TextView nameText = (TextView) mView.findViewById(R.id.name);
            nameText.setText(title);
        }
        public void setUserName(String title) {
            TextView nameText = (TextView) mView.findViewById(R.id.username);
            nameText.setText(title);
        }
        public void setImage(String image) {
            CircularImageView mDisplayImage = (CircularImageView) mView.findViewById(R.id.circleImageView);
            Picasso.get().load(image).into(mDisplayImage);
        }

No results are showing...

I have tried Childevent and ValueEvent I have tried With for loop and without it

Database

edon't image description here

I dont know whats wrong and im tired of trying because there is no error also and I have no idea where I'm going wrong. Can someone please help me out by pointing out the error?

Upvotes: 0

Views: 52

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

Searching in Firebase is always a two-step approach:

  1. Order the data
  2. Filter the data based on that ordering

You're missing step 1 in your code: nowhere do you indicate what to order on.

For example, if you're trying to search the user date by name, your query would become:

mDataabseReference.child(uid)
                  .orderByChild("Name")
                  .startAt(mSearchText).endAt(mSearchText + "\uf8ff")
                  .addValueEventListener(new ValueEventListener() {

Another mistake is that you're looking over dataSnapshot.getChildren() in onChildAdded. But onChildAdded already fires for each individual child node, so the loop is not needed:

 public void onChildAdded(DataSnapshot dataSnapshot, String s) {
     SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
     mSearchList.add(search);
 }

Upvotes: 2

Related Questions