Android Firebase检查孩子中是否存在数据

时间:2019-02-28 16:00:46

标签: java android firebase firebase-realtime-database

我正在开发一个应用程序,允许其用户实时查看特定公司的公共汽车和火车。

当前,公交车或电车的驾驶员通过选择他正在处理的使用电子邮件和密码的路线/线路进行注册,并将其放置在Firebase的特定节点中。例如,如果驱动程序选择“红线”,则其UID将放置在“用户=>驱动程序=>红线=>总线”中。

然后,当驾驶员登录时,他可以切换“我正在工作”开关,该开关将其位置发送给用户。

但是当发生这种情况时,我希望将userID(userUID)放置在分支下的“ driversAvailable”节点中,该分支是UID在“用户=>驱动程序”下列出的路由,但是我正在努力寻找答案。 / p>

void saveGeoFire(){
    final String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("driversAvailable").child("drivers");
    final DatabaseReference driversRouteRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers");

    ValueEventListener driversAvailableEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()) // in Users => Driverrs => RedLine for example
            {
                //Create new Branch in "driversAvailable" => Drivers => RedLine => userID
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    };
driversRouteRef.addListenerForSingleValueEvent(driversAvailableEventListener);
}

我当前的Firebase数据库:

enter image description here

1 个答案:

答案 0 :(得分:0)

好吧,所以经过反复试验,我发现了它,但它并不像我担心的那样复杂。

void saveGeoFire(){
    final DatabaseReference yellowRef = FirebaseDatabase.getInstance().getReference().child("driversAvailable").child("drivers").child("YellowLine");
    final DatabaseReference redRef = FirebaseDatabase.getInstance().getReference().child("driversAvailable").child("drivers").child("RedLine");

    final String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    final DatabaseReference driversRouteRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child("Redline").child("bus");
    final DatabaseReference driversRouteRedRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers");
    ValueEventListener redRefEventListenter = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()) {

                    if(dataSnapshot.child("RedLine").child("bus").getValue().toString().contains(userId)){
                        Toast.makeText(DriverMapActivity.this, "RedLinesuccess", Toast.LENGTH_SHORT).show();
                        GeoFire geoFire = new GeoFire(redRef);
                        geoFire.setLocation(userId, new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
                    }
                    if(dataSnapshot.child("YellowLine").child("bus").getValue().toString().contains(userId)){
                        Toast.makeText(DriverMapActivity.this, "YellowLine success", Toast.LENGTH_SHORT).show();
                        GeoFire geoFire = new GeoFire(yellowRef);
                        geoFire.setLocation(userId, new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
                    }

                }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    driversRouteRedRef.addListenerForSingleValueEvent(redRefEventListenter);
}

My current Database