应用程序未从Firestore检索文档

时间:2020-01-31 17:54:16

标签: java android firebase google-cloud-firestore

我想获取集合中的一些文档。该文档应从文档中获取名称和专业。但是,每次我运行该应用程序时,都会提取文档并将其作为列表显示在recyclerView中(就像在WhatsApp Messenger中一样),但是这些字段不会以TextViews显示。那是{Card CardView的RecyclerView视图显示空白列表,没有文本(即我希望显示的名称和专业名称)

以下是“模型”,“适配器”和“活动”的代码:

模型

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;
}

适配器

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);
    }
}

活动

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();
}

}

这是结果:

Screen shot of the result

Picture of my database structure

1 个答案:

答案 0 :(得分:1)

代码中的问题在于,Doctor类中的吸气剂名称错误。您已定义以下字段:

private String name;
private String specialty;

带有相应的吸气剂:

public String getmDoctorName() {
    return name;
}

public String getmDoctorSpecialty() {
    return specialty;
}

这是不正确的。吸气剂的名称应与您的字段完全相同。请查看正确的命名:

public String getName() {
    return name;
}

public String getrSpecialty() {
    return specialty;
}

关于最后一个:

private String DoctorImageURL;

您的数据库中没有任何值。除此之外,您的财产以大写字母开头,可能会导致其他一些问题,例如: