1

I'm developing an app and in database there are users like doctor and patient. There may be doctors userId in patient datas and patient userId in doctor datas. Because in app doctor will see some information about his/her patient. I develop this chunk of codes, but I could not add patient informations to Arraylists before setting adapter. Here is my codes:

  myRef.child("users")
            .child(firebaseUser.getUid()).child("patients").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for(DataSnapshot ds: dataSnapshot.getChildren()){
                    AddingPatients addingpatient = ds.getValue(AddingPatients.class);

                    if(addingpatient.getConfirm().equals("yes")){

                        myref.child("users").child(addingpatient.getUserId()).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (dataSnapshot.exists()) {
                                    Users patient = dataSnapshot.getValue(Users.class);

                                    name.add(patient.getName()+" "+patient.getSurname());

                                    sicks.add(patient.getSicks());
                                    patientuid.add(patient.getUserId());
                                }

                            }

                            @Override
                            public void onCancelled(DatabaseError error) {
                            }
                        });
                    }
                }

            PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
            rv.setAdapter(adapter);
            rv.setLayoutManager(new LinearLayoutManager(getContext()));

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

When I debug codes I saw that, patient information is adding to lists after setting adapter. So my RecyclerView is empty. How can i fix that problem?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Kemal Aydeniz
  • 119
  • 2
  • 10

2 Answers2

2

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

PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));

Inside the second onDataChange() method like this:

myref.child("users").child(addingpatient.getUserId()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            Users patient = dataSnapshot.getValue(Users.class);

            name.add(patient.getName()+" "+patient.getSurname());

            sicks.add(patient.getSicks());
            patientuid.add(patient.getUserId());
        }
        PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onCancelled(DatabaseError error) {}
});

This is happening because onDataChange() has an asynchronous behavior. Please see here mode details.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

You need to:

  1. Move the following line to top outer onDataChange().

    PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
    
  2. Insert following line before returning from inner onDataChange()

    adapter.notifyDataSetChanged();