简体   繁体   English

应用程序未从 Firestore 检索文档

[英]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 .但是每次我运行应用程序时,文档都会被提取并作为列表显示在 recyclerView 中(就像在 WhatsApp messenger 中一样),但是TextViews没有显示这些字段。 That is the RecyclerView view display blank lists of `CardView with no texts ( ie the name and specialty I wanted it to display)那是RecyclerView视图显示没有文本的 `CardView 空白列表(即我希望它显示的名称和专业)

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:这是结果:

结果的屏幕截图

Picture of my database structure我的数据库结构图

The problem in your code lies in the fact that you have wrong names for your getters in your Doctor class.您的代码中的问题在于,您在Doctor类中为 getter 命名了错误的名称。 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.您的 getter 应该与您的字段完全相同。 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或者

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

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