This method getBedHistory takes an input as list of beds called allBedsWithoutStatus() and a date. I have 34 beds in my allBedsWithoutStatus(). It calls a method called calculateStatus() and then calculates the status, and passes it back as an integer. The results used to create a new array allBedsWithStatus(). The new array allBedsWithStatus only contains the details of my input array (allBedsWithoutStatus)
Status = open = 0; allocated = 1; etc…
calculateStatus includes a firebase call which has an oncomplete listener which makes it an asynchronous method so I therefore added the callbackb which is intended to wait until the method completes before it returns with the status.
The calculateStatus method is rolling through the bedIds and getting the status at the date correctly.
However, on runtime this routine drops straight to bottom of method and returns 0 for every status
when I return to the calling method getBedHistory() (see below) The last element in my arraylist is showing 34 times and status of 0 for that bedId
<pre> 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);
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;
}
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 int calculateStatus(String bedId, Callbackb callbackb) {
System.out.println("This the bed Id before using db call "+ bedId);
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();
}
if (eventDateFromDb.compareTo(dateSelectedFromScreen) <= 0) {
event = history.getString("eventType");
}
}
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;
}
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;
}
What I have tried:
Everything..any help at all would be greatly appreciated