Android Firebase Query for Multiple values

Kunal Shah :

I want to fetch the all uid's of the doctor whose category is "abc"(example) and then store those retrieved uid's into an array list
I want same results as displayed by the below sql query
select uid from doctors where category = "abc";
Here is the database structure

Code for next activity is written below

private void loadDoctors() {

    ArrayList<String> uid = getIntent().getStringArrayListExtra("doctor_list");

    for (int i =0;i<uid.size();i++){
        DatabaseReference doctors = FirebaseDatabase.getInstance().getReference("Doctors").child(uid.get(i));
        Adapter = new FirebaseRecyclerAdapter<DoctorModel, DoctorViewHolder>(
                DoctorModel.class,
                R.layout.doctors_home,
                DoctorViewHolder.class,
                doctors
        ) {
            @Override
            protected void populateViewHolder(final DoctorViewHolder viewHolder, final DoctorModel model, int position) {
                viewHolder.doctor_name.setText(model.getName());

                Glide.with(Doctors.this).load(model.getProfileimage()).into(viewHolder.doctor_image);

                viewHolder.qualification.setText(model.getQualification());

                viewHolder.rating.setText(model.getRating());

            }

        };
        Adapter.notifyDataSetChanged();
        doctors_list.setAdapter(Adapter);
    }
Alex Mamo :

To solve this, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference doctorsRef = rootRef.child("Doctors");
Query categoryQuery = doctorsRef.orderByChild("category").equalTo("Cardiology");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> uidList = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String uid = ds.getKey();
            uidList.add(uid);
        }

        //Do what you need to do with your list
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
categoryQuery.addListenerForSingleValueEvent(valueEventListener);

At the end, you'll have a list full of uids. One more thing to note, because Firebease API is asynchronous, you will be able to use that list only inside onDataChange() method.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=388837&siteId=1