B-rad
B-rad

Reputation: 47

Search FirebaseDatabase for users by username

I am trying to search for users stored in my firebase database by username with a simple search bar (EditText) and when a button is clicked to display that particular user

i have the following code in my findfriends.java file

public class FindFriendsActivity extends AppCompatActivity {

    private EditText seachTextBox;
    private ImageView searchImage;
    private RecyclerView searchResultList;
    private DatabaseReference allUsersDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_friends);

        searchResultList = findViewById(R.id.search_result_list);
        searchResultList.setHasFixedSize(true);
        searchResultList.setLayoutManager(new LinearLayoutManager(this));

        allUsersDatabase = FirebaseDatabase.getInstance().getReference().child("users");

        seachTextBox = findViewById(R.id.searchBox);
        searchImage = findViewById(R.id.searchImage);

        searchImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String seachbox = seachTextBox.getText().toString();

               final Query searchpeople = allUsersDatabase.orderByChild("username").startAt(seachbox)
                        .endAt(seachbox + "\uf8ff");
            }
        });

    }

    @Override
    protected void onStart()
    {
        super.onStart();

        FirebaseRecyclerOptions<FindFriends> options =
                new FirebaseRecyclerOptions.Builder<FindFriends>()
                .setQuery(allUsersDatabase, FindFriends.class)
                .build();

        FirebaseRecyclerAdapter<FindFriends, FindFriendsHolder> adapter =
                new FirebaseRecyclerAdapter<FindFriends, FindFriendsHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull FindFriendsHolder holder, int position, @NonNull FindFriends model)
                    {
                        holder.username.setText(model.getUsername());
                        holder.fullname.setText(model.getFullname());
                        Picasso.get().load(model.getProfileimage()).placeholder(R.drawable.friends_result_icon).fit().into(holder.userimage);
                    }

                    @NonNull
                    @Override
                    public FindFriendsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
                    {
                        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_friends_result_layout,viewGroup,false);
                        FindFriendsHolder viewHolder = new FindFriendsHolder(view);
                        return viewHolder;
                    }
                };

        searchResultList.setAdapter(adapter);
        adapter.startListening();
    }


    public static class FindFriendsHolder extends RecyclerView.ViewHolder
    {

        TextView username, fullname;
        ImageView userimage;

        public FindFriendsHolder(@NonNull View itemView)
        {
            super(itemView);

            username = itemView.findViewById(R.id.username_result);
            fullname = itemView.findViewById(R.id.fullname_result);
            userimage = itemView.findViewById(R.id.friend_result_image);
        }

    }

}///end

I have done some research and found that I need to create a Query and have it set to Startat(Edittext).endAt(EditText) but I am unsure on where to actually insert the query.

with the following code i am able to display every user in the database.

any help would be great!

Upvotes: 1

Views: 230

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

According to your comments you are getting now all users. If you want to get only a single user, please change the following line of code:

Query searchpeople = allUsersDatabase.orderByChild("username").startAt(seachbox)
                    .endAt(seachbox + "\uf8ff");

to

Query searchpeople = allUsersDatabase.orderByChild("username").equalTo(seachbox);

Upvotes: 2

Naufal Prakoso
Naufal Prakoso

Reputation: 90

If you want make it easier, you can use Firestore instead of Realtime DB. But, if you want to still use RealtimeDB, you can create conditional loop like:

if(edtUsername.text.toString == username){
   // Your code
}

Upvotes: 0

Related Questions