简体   繁体   中英

Cannot retrieve data from Firebase Realtime Database Android

I have few tables on Firebase from where I normally retrieve data, but there is one table from where I cannot get data in the same way, although a snapshot of the table has all the values
Fragment from where I get data

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_doctors, container, false);

        mClinicId = mAuth.getUid();

        mDoctorList = new ArrayList<>();

        mFloatingActionButton = v.findViewById(R.id.add_doctor_fab);
        mEmptyListTextView = v.findViewById(R.id.empty_list_tv);

        checkIsListEmpty();

        mDoctorsRecyclerView = v.findViewById(R.id.doctors_rv);
        mDoctorsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),
                RecyclerView.VERTICAL, false));
        
        mAdapter = new DoctorAdapter(mDoctorList, getActivity());
        mDoctorsRecyclerView.setAdapter(mAdapter);


        DatabaseReference mReference = FirebaseDatabase.getInstance().getReference().child("employees");
        mReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {
                    Log.d("myLog", "onDataChange: snapshot " + snapshot);
                    for (DataSnapshot doctorSnapshot : snapshot.getChildren()){
                        Doctor doctor = doctorSnapshot.getValue(Doctor.class);
                        mDoctorList.add(doctor);
                        Log.d("myLog", "onDataChange: doctor " + doctor.getEmail());
                    }                
                    mAdapter.setDoctors(mDoctorList);
                    mAdapter.notifyDataSetChanged();
                    checkIsListEmpty();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.d("myLog", "onCancelled: " + error.getMessage());
            }
        });



        mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClinicId != null) {
                    startActivity(RegistrationActivity.newInstance(getActivity(),
                            true, mClinicId));
                }
            }
        });

        return v;
    }


Doctor.java

    private String mUID;
    private String mEmail;
    private String mSurname;
    private String mName;
    private String mPatronymic;
    private Date mBirthDate;
    private String mClinicId;
    private String mPosition;

    public Doctor() {
    }
    
    public Doctor(String UID, String email, String surname, String name, String patronymic, Date birthDate, String clinicId, String position) {
        mUID = UID;
        mEmail = email;
        mSurname = surname;
        mName = name;
        mPatronymic = patronymic;
        mBirthDate = birthDate;
        mClinicId = clinicId;
        mPosition = position;
    }

    public String getPosition() {
        return mPosition;
    }

    public String getUID() {
        return mUID;
    }

    public String getEmail() {
        return mEmail;
    }

    public String getSurname() {
        return mSurname;
    }

    public String getName() {
        return mName;
    }

    public String getPatronymic() {
        return mPatronymic;
    }

    public Date getBirthDate() {
        return mBirthDate;
    }

    public String getClinicId() {
        return mClinicId;
    }
}


As you can see in the snapshot there is data, but when they are initialized, they are equal to null
Logs

2021-05-02 19:03:14.510 20833-20833/com.rpkeffect.android.rpkpolyclinik D/myLog: onDataChange: snapshot DataSnapshot { key = employees, value = {-MZhhQb9EaQkjOp8GLEm={uid=frrvDpFDCiVBPxHkoq328XT0XwV2, clinicId=7iNGGrbV4cXU1NnyVYy0rNIUE0w2, patronymic=dudhdh, surname=dhdjhdjdh, name=dudhd, position=dyduxh, birthDate={date=2, hours=0, seconds=0, month=4, timezoneOffset=-240, year=94, minutes=0, time=767822400000, day=1}, email=mail@mail.ru}, -MZhi2_TqCZvabIekppj={uid=IZsIIxdlw1f7E66xoNluzyc9IPT2, clinicId=frrvDpFDCiVBPxHkoq328XT0XwV2, patronymic=djdnx, surname=djdnxxn, name=xjxbx, position=jxjxnxx, birthDate={date=2, hours=0, seconds=0, month=4, timezoneOffset=-240, year=93, minutes=0, time=736286400000, day=0}, email=mail@mail.ru}} }
2021-05-02 19:03:14.512 20833-20833/com.rpkeffect.android.rpkpolyclinik D/myLog: onDataChange: doctor null
2021-05-02 19:03:14.512 20833-20833/com.rpkeffect.android.rpkpolyclinik D/myLog: onDataChange: doctor null

If I trying to get data from the other table, I can get it successfully


Firebase table

https://i.stack.imgur.com/qkjvB.png

The problem was that I didn't generate setters on my Doctor.java class

Doctor.java

public class Doctor {
    private String mUID;
    private String mEmail;
    private String mSurname;
    private String mName;
    private String mPatronymic;
    private Date mBirthDate;
    private String mClinicId;
    private String mPosition;

    public Doctor() {
    }

    public Doctor(String UID, String email, String surname, String name, String patronymic, Date birthDate, String clinicId, String position) {
        mUID = UID;
        mEmail = email;
        mSurname = surname;
        mName = name;
        mPatronymic = patronymic;
        mBirthDate = birthDate;
        mClinicId = clinicId;
        mPosition = position;
    }

    public String getPosition() {
        return mPosition;
    }

    public String getUID() {
        return mUID;
    }

    public String getEmail() {
        return mEmail;
    }

    public String getSurname() {
        return mSurname;
    }

    public String getName() {
        return mName;
    }

    public String getPatronymic() {
        return mPatronymic;
    }

    public Date getBirthDate() {
        return mBirthDate;
    }

    public String getClinicId() {
        return mClinicId;
    }

    public void setUID(String UID) {
        mUID = UID;
    }

    public void setEmail(String email) {
        mEmail = email;
    }

    public void setSurname(String surname) {
        mSurname = surname;
    }

    public void setName(String name) {
        mName = name;
    }

    public void setPatronymic(String patronymic) {
        mPatronymic = patronymic;
    }

    public void setBirthDate(Date birthDate) {
        mBirthDate = birthDate;
    }

    public void setClinicId(String clinicId) {
        mClinicId = clinicId;
    }

    public void setPosition(String position) {
        mPosition = position;
    }

When you try to map a node from Firebase Realtime Database into an object of your "Doctor", the name of the fields that exist in your class must match the name of your properties that exist in your database. Unfortunately, in your case, the fields don't match. See, the fields in your class start with "m", while in the database doesn't start with "m", which is not correct.

To solve this, you have two options, you either change the name of your properties in the class to match the one in the database, or you can use an annotation in front of the getters. For example, if you have a field called "mEmail" and the property in the database is called "email", your getter should look like this:

@PropertyName("email")
public String getEmail() {
    return mEmail;
}

In this way, you tell the compiler to look for a property called "email" and not "mEmail".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM