JAtoms
JAtoms

Reputation: 669

App Not Retrieving Documents From Firestore

I wanted to fetch some documents in a collection. The document should fetch name and specialty from a document. But each time I run the app, the documents are fetched and shown in the recyclerView as a list (just like in WhatsApp messenger), but the fields are not showing in by TextViews. That is the RecyclerView view display blank lists of `CardView with no texts ( ie the name and specialty I wanted it to display)

Below is the code for the Model, Adapter, and Activity:

THE MODEL

public class Doctor {

private String name;
private String DoctorImageURL;
private String specialty;

public Doctor() {
    //empty constructor needed
}

public Doctor(String name, String specialty) {
    this.name = name;
    this.specialty = specialty;
}

public String getmDoctorName() {
    return name;
}

public String getmDoctorSpecialty() {
    return specialty;
}

THE ADAPTER

public class DoctorAdapter extends FirestoreRecyclerAdapter<Doctor, DoctorAdapter.DoctorHolder> {


public DoctorAdapter(@NonNull FirestoreRecyclerOptions<Doctor> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull DoctorHolder doctorHolder, int i, @NonNull Doctor doctor) {

    doctorHolder.Name.setText(doctor.getmDoctorName());
    doctorHolder.Specialty.setText(doctor.getmDoctorSpecialty());

    //doctorHolder.DocImage.setImageResource(unifiedModel.getmDoctorImageURL());

    //Load images here

}

@NonNull
@Override
public DoctorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.doctor_list_view_items,
           parent, false);

    return new DoctorHolder(view);
}

class DoctorHolder extends RecyclerView.ViewHolder{

    TextView Name, Specialty;
    ImageView DocImage;


    public DoctorHolder(@NonNull View itemView) {
        super(itemView);

        Name = itemView.findViewById(R.id.doctor_name);
        Specialty = itemView.findViewById(R.id.doctor_specialty);
        DocImage = itemView.findViewById(R.id.doctor_image);
    }
}

THE ACTIVITY

public class DoctorActivity extends AppCompatActivity {

FirebaseAuth firebaseAuth;
private FirebaseFirestore rootReference = FirebaseFirestore.getInstance();
private CollectionReference doctorRef = rootReference.collection("doctor");

private DoctorAdapter doctorAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_doctor);

    firebaseAuth = FirebaseAuth.getInstance();

    setUpRecyclerView();
}

private void setUpRecyclerView() {

    // Query FireStore and order items by name in Ascending order
    Query query = doctorRef.orderBy("name", Query.Direction.ASCENDING);

    FirestoreRecyclerOptions<Doctor> options = new FirestoreRecyclerOptions.Builder<Doctor>()
            .setQuery(query, Doctor.class)
            .build();

    doctorAdapter = new DoctorAdapter(options);

    RecyclerView recyclerView = findViewById(R.id.doctor_recycler_view);
    recyclerView.setHasFixedSize(true);  // For performance reasons
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(doctorAdapter);

}

@Override
protected void onStart() {
    super.onStart();
    doctorAdapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    doctorAdapter.stopListening();
}

}

this is the result:

Screen shot of the result

Picture of my database structure

Upvotes: 0

Views: 51

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

The problem in your code lies in the fact that you have wrong names for your getters in your Doctor class. You have defined the following fields:

private String name;
private String specialty;

With the corresponding getters:

public String getmDoctorName() {
    return name;
}

public String getmDoctorSpecialty() {
    return specialty;
}

Which is not correct. You getters should be named exactly as your fields. Please see the correct naming:

public String getName() {
    return name;
}

public String getrSpecialty() {
    return specialty;
}

Regarding the last one:

private String DoctorImageURL;

There is no value in your database. Besides that, your property starts with a capital letter which might lead to some other issues like:

Or

Upvotes: 1

Related Questions