【发布时间】: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 还是有点陌生,所以如果有人能帮助指出我的错误,我将不胜感激
【问题讨论】: