我正在尝试在另一个firebase查询中执行firebase查询,如嵌套for循环,但外部查询在内部查询开始之前完全运行。我该如何实现这一目标?我是一个android和firebase初学者
外部查询
private void retrieveMarkerDetails(final String key) {
databaseReference = mDatabase.getReference();
databaseReference.child("Parking Lots").child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
parking_lot_details = dataSnapshot.getValue(Parking_Lot_Details.class);
Marker marker;
if (markers.size() != 0) {
marker = markers.get(key);
String title = parking_lot_details.getLotName();
//Calculate Remaining space
spaceAvailable();
Log.i(“剩余空间avai”,String.valueOf(parking_lot_details.getAvailableSpace()));
String snippet = "Available space " + parking_lot_details.getAvailableSpace();
Log.i("remaining space", String.valueOf(parking_lot_details.getCapacity()));
// String snippet = "Available space " + parking_lot_details.getCapacity();
marker.setTitle(title);
marker.setSnippet("\t" + snippet);
Log.i("key : ", key);
内部查询 这是应该运行的内部查询。
private void spaceAvailable() {
spaceAvailable = new int[1];
//Strip email of dots and use it to access requests, since the requests use the emails
//of lot owners as keys
String lot_email= parking_lot_details.getEmail().replace(".","");
Log.i("lot_email",lot_email);
databaseReference.child("Requests").child(lot_email).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
long requestsMade = dataSnapshot.getChildrenCount();
Log.i("Requests ", dataSnapshot.toString());
Log.i("RequestsMade", Long.toString(requestsMade));
spaceAvailable[0] = (int) (Integer.parseInt(parking_lot_details.getCapacity()) - requestsMade);
parking_lot_details.setAvailableSpace(String.valueOf(spaceAvailable[0]));
Log.i("Space Available ", String.valueOf(spaceAvailable[0]));
Log.i("Space Available in met ", parking_lot_details.getAvailableSpace());
}else{
//if the email does not appear under requests then we assume that no requests have
//been made and the available space is equal to the lots capacity
parking_lot_details.setAvailableSpace(parking_lot_details.getCapacity());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
数据库
LOG
答案 0 :(得分:2)
为了使其有效,请更改第二个查询:
databaseReference.child("Requests").child(lot_email).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
long requestsMade = dataSnapshot.getChildrenCount();
Log.d("TAG", dataSnapshot.getKey() + " / No. of childrens: " + requestsMade);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
long childRequestsMade = ds.getChildrenCount();
String lot_email = ds.child("lot_email").getValue(String.class);
Log.d("TAG", lot_email);
}
}else{
//if the email does not appear under requests then we assume that no requests have
//been made and the available space is equal to the lots capacity
parking_lot_details.setAvailableSpace(parking_lot_details.getCapacity());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
要显示这些子项内的值,您需要使用for循环并使用getChildren()
方法。在此示例中,它仅打印lot_email
。