beastlyCoder
beastlyCoder

Reputation: 2401

Searching Contents in Firebase Live Database

When I search for items in my firebase database, I am not getting an error but instead no value at all. When going through the debugger, the values I have in the editText are infact being set and when searching for those values in the Query, they aren't showing up:

final EditText t = (EditText) findViewById(R.id.DjNameET);

        mProfileDatabase = FirebaseDatabase.getInstance().getReference();

        t.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if(i == EditorInfo.IME_ACTION_SEARCH)
                {
                    searchData = t.getText().toString();


                    return true;
                }
                return false;
            }
        });

        ImageButton imgB= (ImageButton) findViewById(R.id.searchBtn);

        imgB.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view)
            {
                query = mProfileDatabase.child("djprofile").orderByChild("djName")
                        .startAt(searchData)
                        .endAt(searchData + "\uf8ff");

                FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
                        .setQuery(query, DjProfile.class)
                        .build();
                firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
                {

                    @NonNull
                    @Override
                    public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);

                        return new ResultsViewHolder(view);
                    }

                    @Override
                    protected void onBindViewHolder(@NonNull ResultsViewHolder holder, int position, @NonNull DjProfile model) {
                        holder.setDjProfile(model);
                    }
                };
                firebaseRecyclerAdapter.startListening();
                recyclerView.setAdapter(firebaseRecyclerAdapter);
            }
        });

Here is what the live database looks like:

enter image description here

In my edit text when I search for "DjRockSauce" no results are shown within the recyclerview, I have a feeling this is because I am not getting the correct path into the query, if this is the case how do I correctly get the file paths for searching and displaying results?

Upvotes: 0

Views: 94

Answers (2)

beastlyCoder
beastlyCoder

Reputation: 2401

Figured out with the help of @Frank van Puffelen, that my issue was I was calling searchText, and it was returning null. When in fact I needed to pass the physical EditText name's text into the search query as follows:

            query = mProfileDatabase.child("djprofile").orderByChild("djName")
                    .startAt(t.getText().toString()).endAt(t.getText().toString() + "\uf8ff");

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

When you have a problem like this, the right approach is to try to find where the problem is, and where it isn't.

The first step could be to see if you can show the unfiltered profiles.

query = mProfileDatabase.child("djprofile");

If this shows all profiles, you know that the path you build is correct.

If that is the case, I'd try with a hard-coded search string:

query = mProfileDatabase.child("djprofile").orderByChild("djName")
        .startAt("Dr")
        .endAt("Dr\uf8ff");

If that works too, the problem is in how you get the search term, and you should be looking into that.

Upvotes: 2

Related Questions