0

enter image description here

As in my table user having the same profession=doctor.

I want to select all users having the same profession and want to show them in recycler view.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • You must use a `for` loop to read all profession nodes from each UID, then use an `if (profession.equals("doctor")` statement to show them in the recycler – Adrixb Jun 14 '21 at 09:10

1 Answers1

0

To be able to get all the users that a profession set as a doctor, you need to see a Query. So please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
Query queryByProffesion = usersRef.orderByChild("proffesion").equalTo("doctor");
    queryByProffesion.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String name = ds.child("name").getValue(String.class);
                Log.d(TAG, name);
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be the name of all users who are doctors. For displaying the results in a RecyclerView, please check my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi Alex,Sir i tried but it didnt work for me kindly if you help me out – Iqrar Ijaz Jun 17 '21 at 17:42
  • What exactly didn't help? Without seeing the code, I cannot be much of a help. Please edit your question and add your code, and tell what exactly doesn't work. – Alex Mamo Jun 18 '21 at 07:05