【问题标题】:App Not Retrieving Documents From Firestore应用程序未从 Firestore 检索文档
【发布时间】:2024-05-19 23:05:01
【问题描述】:

我想获取集合中的一些文档。该文档应从文档中获取名称和专业。但是每次我运行该应用程序时,文档都会被提取并显示在 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();
}

}

这是结果:

Picture of my database structure

【问题讨论】:

  • 请将您的数据库结构添加为截图。
  • @Alex Mamo 我已经添加了数据库结构的图片

标签: java android firebase google-cloud-firestore


【解决方案1】:

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

private String name;
private String specialty;

使用相应的getter:

public String getmDoctorName() {
    return name;
}

public String getmDoctorSpecialty() {
    return specialty;
}

这是不正确的。你的吸气剂应该被命名为你的字段。请看正确的命名:

public String getName() {
    return name;
}

public String getrSpecialty() {
    return specialty;
}

关于最后一个:

private String DoctorImageURL;

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

或者

【讨论】: