Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在for循环内调用方法时,ArrayList被重写,并且该方法不执行firebase调用_Java_For Loop_Google Cloud Firestore_Callback - Fatal编程技术网

Java 在for循环内调用方法时,ArrayList被重写,并且该方法不执行firebase调用

Java 在for循环内调用方法时,ArrayList被重写,并且该方法不执行firebase调用,java,for-loop,google-cloud-firestore,callback,Java,For Loop,Google Cloud Firestore,Callback,此方法getBedHistory将输入作为名为allBedsWithoutStatus()的床列表和日期。我的allBedsWithoutStatus()里有34张床。它调用一个名为calculateStatus()的方法,然后计算状态,并将其作为整数传回。用于创建新数组allbestWithStatus()的结果。新数组AllBestWithStatus仅包含我的输入数组(AllBestWithOutStatus)的详细信息 状态=打开=0;分配=1;等等 calculateStatus包含一

此方法getBedHistory将输入作为名为allBedsWithoutStatus()的床列表和日期。我的allBedsWithoutStatus()里有34张床。它调用一个名为calculateStatus()的方法,然后计算状态,并将其作为整数传回。用于创建新数组allbestWithStatus()的结果。新数组AllBestWithStatus仅包含我的输入数组(AllBestWithOutStatus)的详细信息

状态=打开=0;分配=1;等等

calculateStatus包含一个firebase调用,该调用具有一个oncomplete侦听器,使其成为一个异步方法,因此我添加了callbackb,它打算等到该方法完成后再返回状态

calculateStatus方法在BEDID中滚动,并正确获取日期的状态

然而,在运行时,这个例程直接下降到方法的底部,并为每个状态返回0

当我返回到调用方法getBedHistory()时(请参见下文),arraylist中的最后一个元素显示了34次,该bedId的状态为0

public void getBedHistory() {
        int statusCode = 0;
        for (int u = 0; u < allBedsWithoutStatus.size(); u++) {
            bedIdString = allBedsWithoutStatus.get(u).getBedId();
            wardIdString = allBedsWithoutStatus.get(u).getWard();
            System.out.println("Before calculate This the bed id and ward id " + u + "  " + bedIdString + " " + wardIdString);


                 bedStatusAtDate  =  calculateStatus(bedIdString, new Callbackb() {
                @Override
                public void callb() {


                    System.out.println("This is the bed status at date  = " + BedStatusChartsForDate.this.bedStatusAtDate);

                    // Put as much as the logic we can here
                    // all i need is the status of each bed
                    switch (wardIdString) {
                        case "St Johns":
                            wardCode = 0;
                            break;
                        case "St Marys":
                            wardCode = 1;
                            break;
                        case "St Pauls":
                            wardCode = 2;
                            break;
                        case "St Magz":
                            wardCode = 3;
                            break;
                        case "St Joes":
                            wardCode = 4;
                            break;
                        default:
                            wardCode = 5;
                    }  // end Switch for Ward

                    bedStatus[0] = statusCode;
                    bedStatus[1] = wardCode;
                    bedDetails.setBedId(bedIdString);
                    bedDetails.setWard(wardId);
                    bedDetails.setStatus(statusCode);
                    BedStatusChartsForDate.this.allBedsWithStatus.add(bedDetails);
                    int size =  allBedsWithStatus.size() - 1;
                    System.out.println("List of all beds with status added end of getBedHistory = " + "  " + size + " " + allBedsWithStatus.get(size).getBedId() + allBedsWithStatus.get(size).getWard() + " " + allBedsWithStatus.get(size).getStatus());

                }
            });
        }
  
public void getBedHistory(){
int statusCode=0;
对于(int u=0;u
//方法获取beds集合中的所有床位,然后在下一个方法中传入以获取床位历史记录
public int calculateStatus(字符串bedId,Callbackb Callbackb){
System.out.println(“这是使用db调用之前的bed Id”+BEID);
//System.out.println(“这应该是返回的详细信息”+.getAllBedDetails());
//现在获取床的历史记录以获取状态
db.收集(“床”)
.文件(bedId)
.收藏(“bedHistory4”)
.orderBy(“日期和时间”)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull task1){
if(task1.issusccessful()){
字符串事件=”;
字符串eventDateString=“”;
Date eventDateFromDb=新日期();
Date Date SelectedFromScreen=新日期();
对于(QueryDocumentSnapshot历史记录:task1.getResult()){
eventDateString=history.getString(“dateAndTime”);
SimpleDateFormat=新的SimpleDateFormat(“yyyy-MM-dd HH:MM:ss”);
SimpleDataFormat dateFormatter=新的SimpleDataFormat(“dd-MM-yyyy-HH:MM:ss”);
试一试{
eventDateFromDb=format.parse(eventDateString);
dateSelectedFromScreen=dateFormatter.parse(dateFromDateSelected);
}捕获(解析异常){
e、 printStackTrace();
}
//System.out.println(“这是返回的日期时间”+:“+eventDateFromDb”);
//System.out.println(“这是日期时间字符串“+”:“+dateSelectedFromScreen”);
//在所选日期之前或之前存储事务的eventType

如果(eventDateFromDb.compareTo)(dateSelectedFromScreen)在调用oncomplete方法之前从calculateStatus返回状态代码。但是您的代码不完整:您没有显示状态代码、床位状态或床位详细信息的声明位置。@tgdavies感谢您的回复int statusCode;ArrayList AllBestWithStatus=new ArrayList();int[]bedStatus=new int[]{0,0};BedInfo beddails=new BedInfo();所有这些都是全局的,您是正确的,它在执行onCompleteListener之前返回。我输入
// Method here to get all the beds from the beds collection to then to be passed in to get the bed history in the next method
public int calculateStatus(String bedId, Callbackb callbackb) {
    System.out.println("This the bed Id before using db call "+ bedId);
    //   System.out.println("this should be details returned "+   .getAllBedDetails());
    //Now get the history of the bed to get the status
    db.collection("bed")
            .document(bedId)
            .collection("bedHistory4")
            .orderBy("dateAndTime")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task1) {
                    if (task1.isSuccessful()) {
                        String event = "";
                        String eventDateString = "";
                        Date eventDateFromDb = new Date();
                        Date dateSelectedFromScreen = new Date();
                        for (QueryDocumentSnapshot history : task1.getResult()) {
                            eventDateString = history.getString("dateAndTime");
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                            try {
                                eventDateFromDb = format.parse(eventDateString);
                                dateSelectedFromScreen = dateFormatter.parse(dateFromDateSelected);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            //   System.out.println("This is date time returned " + ": " + eventDateFromDb);
                            //   System.out.println("This is date time string  " + ": " + dateSelectedFromScreen);

                            //Storing the eventType for transactions before or equal to the selectedDate
                            if (eventDateFromDb.compareTo(dateSelectedFromScreen) <= 0) {
                                event = history.getString("eventType");
                            }
                        }
                        // end For loop
                        // now use the event to set the status in the bedStatus array.
                        switch (event) {
                            case "Added bed":
                            case "Bed allocated to ward":
                            case "Bed is now open":
                            case "Bed is cleaned – ready for next patient":
                                statusCode = 0;
                                break;
                            case "Bed allocated - patient on way":
                                statusCode = 1;
                                break;
                            case "Patient in bed in ward":
                                statusCode = 2;
                                break;
                            case "Bed ready for cleaning":
                                statusCode = 3;
                                break;
                            default:
                                statusCode = 4;
                                System.out.println("This bed is the bed not created yet" + bedId);
                                break;
                        }   // end Switch for bedStatus

                        System.out.println("Bed details added to all beds with status " + bedId +  " " + " " + statusCode);
                        callbackb.callb();
                    } else {
                        Log.d(TAG, "Error getting documents: ", task1.getException());
                    }

                }
            });
    return statusCode;