繁体   English   中英

应用程序未从 Firestore 检索文档

[英]App Not Retrieving Documents From Firestore

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

下面是模型、适配器和活动的代码:

该模型

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

}

这是结果:

结果的屏幕截图

我的数据库结构图

您的代码中的问题在于,您在Doctor类中为 getter 命名了错误的名称。 您已定义以下字段:

private String name;
private String specialty;

使用相应的吸气剂:

public String getmDoctorName() {
    return name;
}

public String getmDoctorSpecialty() {
    return specialty;
}

这是不正确的。 您的 getter 应该与您的字段完全相同。 请参阅正确的命名:

public String getName() {
    return name;
}

public String getrSpecialty() {
    return specialty;
}

关于最后一个:

private String DoctorImageURL;

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

或者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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