gpt4 book ai didi

android - 嵌套的 Firebase 查询

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:56 24 4
gpt4 key购买 nike

我正在尝试在另一个 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("可用剩余空间", 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) {

}
});



}

数据库

enter image description here

日志

enter image description here

after trying suggested fix

最佳答案

为了让它工作,请像这样更改第二个查询:

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

关于android - 嵌套的 Firebase 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com