首页 > 解决方案 > 从具有另一个对象数组列表属性的对象数组列表中获取值

问题描述

对于我的大学项目,我正在尝试实现一个分段的 recyclerview,其中节标题为日期,约会列表为内容,在每一行约会中我想显示一些字段,其中我创建了 apnmntDetails 类为以下:

public class apnmtDetails {
private String BarberName;
private String Barbershop;
private String ServiceName;
private String ServiceType;
private String Servicestatus;
private String ServicePrice;
private String ServiceTime;

public apnmtDetails(String barberName, String barbershop,
                    String serviceName, String serviceType,
                    String servicestatus, String servicePrice,
                    String serviceTime) {
    BarberName = barberName;
    Barbershop = barbershop;
    ServiceName = serviceName;
    ServiceType = serviceType;
    Servicestatus = servicestatus;
    ServicePrice = servicePrice;
    ServiceTime = serviceTime;
}

public String getBarberName() {
    return BarberName;
}

public void setBarberName(String barberName) {
    BarberName = barberName;
}

public String getBarbershop() {
    return Barbershop;
}

public void setBarbershop(String barbershop) {
    Barbershop = barbershop;
}

public String getServiceName() {
    return ServiceName;
}

public void setServiceName(String serviceName) {
    ServiceName = serviceName;
}

public String getServiceType() {
    return ServiceType;
}

public void setServiceType(String serviceType) {
    ServiceType = serviceType;
}

public String getServicestatus() {
    return Servicestatus;
}

public void setServicestatus(String servicestatus) {
    Servicestatus = servicestatus;
}

public String getServicePrice() {
    return ServicePrice;
}

public void setServicePrice(String servicePrice) {
    ServicePrice = servicePrice;
}

public String getServiceTime() {
    return ServiceTime;
}

public void setServiceTime(String serviceTime) {
    ServiceTime = serviceTime;
}

@Override
public String toString() {
    return "apnmtDetails{" +
            "BarberName='" + BarberName + '\'' +
            ", Barbershop='" + Barbershop + '\'' +
            ", ServiceName='" + ServiceName + '\'' +
            ", ServiceType='" + ServiceType + '\'' +
            ", Servicestatus='" + Servicestatus + '\'' +
            ", ServicePrice='" + ServicePrice + '\'' +
            ", ServiceTime='" + ServiceTime + '\'' +
            '}';
}}

之后,我创建了一个section类,其中包含sectionName和sectionItem的字段,其中sectionitem是apnmntDetails类的arraylist,代码如下:

public class Section {

private String SectionName;
private ArrayList<apnmtDetails> SectionItem;

public Section(String sectionName, ArrayList<apnmtDetails> sectionItem) {
    SectionName = sectionName;
    SectionItem = sectionItem;
}

public String getSectionName() {
    return SectionName;
}

public void setSectionName(String sectionName) {
    SectionName = sectionName;
}

public ArrayList<apnmtDetails> getSectionItem() {
    return SectionItem;
}

public void setSectionItem(ArrayList<apnmtDetails> sectionItem) {

    this.SectionItem.addAll(sectionItem);
}

@Override
public String toString() {
    return "Section{" +
            "SectionName='" + SectionName + '\'' +
            ", SectionItem=" + SectionItem +
            '}';
}}

现在,在活动中,我通过从 firebase firestore 查询来初始化所有值:

 private void initData(){

    db.collection("appointmentsColl").document(UserId)
            .collection("Date")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {

                            db.collection("appointmentsColl").document(UserId)
                                    .collection("Date").document(document.getId())
                                    .collection("appointmentsID")
                                    .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                    for (DocumentSnapshot querysnapshot: task.getResult()){
                                        apnmtDetails details = new apnmtDetails(querysnapshot.getString("barber"),
                                                querysnapshot.getString("barber"),
                                                querysnapshot.getString("name"),
                                                querysnapshot.getString("type"),
                                                querysnapshot.getString("status"),
                                                querysnapshot.getString("price"),
                                                querysnapshot.getString("time slot"));

                                        apnmntList.add(details);
                                        Log.i("apnmntList", apnmntList.toString());

                                    }
                                }
                            });

                            sectionList.add(new Section(document.getString("date"),apnmntList));

                        }

                        for(int i = 0; i<sectionList.size(); i++)
                        Log.i("sectionList", sectionList.get(i).toString());

                    }else{

                        Toast.makeText(HomePage.this,"failed",Toast.LENGTH_SHORT).show();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.i("Check", e.toString() );
        }
    });
}

问题是当我尝试打印 sectionlist 的值时,只有 sectionName 正确显示,而 appntmntDetails 类的对象没有按我想要的那样出现,这是 logcat:

2021-03-04 14:57:06.748 4678-4678/com.example.homebarberv1 I/sectionList: Section{SectionName='4/AUG/2021', SectionItem=[]}
2021-03-04 14:57:07.029 4678-4678/com.example.homebarberv1 I/apnmntList: [apnmtDetails{BarberName='vAN7LYKoddRX2cQlogQtStOueKt2', Barbershop='vAN7LYKoddRX2cQlogQtStOueKt2', ServiceName='normal cut', ServiceType='Normal', Servicestatus='On hold', ServicePrice='4', ServiceTime='07:00 to 07:30'}]

firebase 不是问题,因为正确检索了该值,如带有 apnmntList 标记的 logcat 中所示,但是当使用 servicelist arraylist 打印该值时,该值没有出现。

我对Java还是有点陌生​​和陌生,所以如果有人能帮助指出我的错误,我将不胜感激

标签: javaandroidarraylist

解决方案


您应该sectionList.add(new Section(document.getString("date"),apnmntList));在 onComplete() 回调方法内部调用。因为这是一个回调方法被挂起和控制跳转到回调代码块后执行代码。您应该在 for 循环之前初始化 apnmntList 的另一件事,因为必须为每个部分保存新列表。我已经对 onComplete() 方法进行了更改,请看一下:

@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
    apnmntList = new ArrayList();
    for (DocumentSnapshot querysnapshot: task.getResult()){
        apnmtDetails details = new apnmtDetails(querysnapshot.getString("barber"),
                querysnapshot.getString("barber"),
                querysnapshot.getString("name"),
                querysnapshot.getString("type"),
                querysnapshot.getString("status"),
                querysnapshot.getString("price"),
                querysnapshot.getString("time slot"));

        apnmntList.add(details);
        Log.i("apnmntList", apnmntList.toString());

    }
    sectionList.add(new Section(document.getString("date"),apnmntList));
}

推荐阅读