【问题标题】:getting the value from an arraylist of objects that has an attribute of another object arraylist从具有另一个对象数组列表属性的对象数组列表中获取值
【发布时间】:2021-03-04 07:52:31
【问题描述】:

对于我的大学项目,我正在尝试实现一个分段的 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 类的对象并没有像我想要的那样出现, 这是日志猫:

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 还是有点陌生​​,所以如果有人能帮助指出我的错误,我将不胜感激

【问题讨论】:

    标签: java android arraylist


    【解决方案1】:

    你应该在 onComplete() 回调方法中调用sectionList.add(new Section(document.getString("date"),apnmntList));。因为这是一个回调方法被挂起和控制跳转到回调代码块后执行代码。您应该在 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));
    }
    

    【讨论】:

    • 您好先生,我尝试实施您推荐的修复程序,当我将 logcat 放在 for 循环中时,我能够打印 serviceList,但是当我尝试在 onComplete 之外打印它时,结果是空的,我该如何解决这个问题?
    • 在 onComplete() 中是否正确打印?看,回调方法就像异步过程,所以你不应该在onComplete之外准备sectionList。
    • 好的先生,非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多