Manuel Pap
Manuel Pap

Reputation: 1399

Firebase retrieve and assign data from parent and children to class

I have the following firebase class. Patients and users(doctor, hospital stuff) is a many to many relation. enter image description here

Patients Class

public class Patients {
    public String name;
    public String surname;
    public ArrayList<String> users;
}

And this is the way I iterated through the data without the users (field of table patients NOT the table) field on firebase database on patients table.

database.child("patients").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot patient : dataSnapshot.getChildren()){
            Patients p = patient.getValue(Patients.class);
            //use p object
            listViewAdapter.notifyDataSetChanged();
        }

        patientsList.setAdapter(listViewAdapter);
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

Of course this doesn't work with the current format of patients table because the children of users (field of table patients NOT the table, needs another loop) and I get that. The thing that I don't get is how can I associate the Patients class with patients table when I have to do another loop. I can't figure a way for Patients p = patient.getValue(Patients.class) to work with another field that needs iteration.

My goal is the object p of Patients class to contain for example name = Mark, surname = Marks , users[] = [y92pZ4rynpUFSOm3MrzQncimu153,...]. I don't how the pojo should be structured and what's the proper way to iterate the children so users ArrayList will contain all the users field for each patient and using something like Patients p = patient.getValue(Patients.class)

Upvotes: 0

Views: 1002

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Having the pojo class with public fields and witout the no argument constructor and the coresponding setters and getters is completely wrong! All your fileds need to be private and you need to access them only trough public setters and getters. Remember, that the no argument constructor is mandatory for Firebase. That's why you cannot map that node. Add them to your pojo class and your problem will be solved.

Upvotes: 1

Related Questions