检索Firebase和Android Studio中驾驶员的passengerRequest节点内的所有乘客ID(用户ID)

时间:2018-08-07 15:49:56

标签: java android firebase firebase-realtime-database

我正在尝试将其检索并转换为字符串,如下图所示,该ID已保存在驾驶员的passengerRequest中:

https://imgur.com/a/F6matnZ

这是我的getAssignPassenger

private void getAssignedPassenger() {
    String driverID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(driverID).child("passengerRequest");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                passengerID = dataSnapshot.getValue().toString();
                getAssignPassengerPickUpLocation();
            }else{
                passengerID = "";
                if(pickupMarker !=null){
                    pickupMarker.remove();
                }
                if(AssignPassengerPickupLocationListener!= null){
                AssignPassengerPickupLocationref.removeEventListener(AssignPassengerPickupLocationListener);
            }}
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

我的问题在这里

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(driverID).child("passengerRequest");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                passengerID = dataSnapshot.getValue().toString();
                getAssignPassengerPickUpLocation();

在passengerID中,它应该检索保存在其中的每个用户ID ... 我该怎么办?

下一个问题是...如果我成功检索了用户ID,以及如何继续找到用户ID,这是节点的图片:

https://imgur.com/1TQQ4Ih

我已经为它启动了代码

这是我的getAssignPassengerLocation

private void getAssignPassengerPickUpLocation() {

    AssignPassengerPickupLocationref = FirebaseDatabase.getInstance().getReference().child("passengerRequest").child(passengerID).child("l");
    AssignPassengerPickupLocationListener = AssignPassengerPickupLocationref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()&&!passengerID.equals("")) {
                List<Object> map = (List<Object>) dataSnapshot.getValue();
                double locationLat = 0;
                double locationLong = 0;

                if (map.get(0) != null) {
                    locationLat = Double.parseDouble(map.get(0).toString());
                }
                if (map.get(1) != null) {
                    locationLong = Double.parseDouble(map.get(1).toString());
                }
                LatLng driverLatLng = new LatLng(locationLat, locationLong);

                mMap.addMarker(new MarkerOptions().position(driverLatLng).title("Pickup Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_pickup_marker)));

在此区块中我应该做什么

AssignPassengerPickupLocationref = FirebaseDatabase.getInstance().getReference().child("passengerRequest").child(passengerID).child("l");
    AssignPassengerPickupLocationListener = AssignPassengerPickupLocationref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()&&!passengerID.equals("")) {
                List<Object> map = (List<Object>) dataSnapshot.getValue();
                double locationLat = 0;
                double locationLong = 0;

是否可以将节点存储在数组字符串中?

1 个答案:

答案 0 :(得分:1)

第一个

您必须在快照中获取快照子代

private void getAssignedPassenger() {
    String driverID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(driverID).child("passengerRequest");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                List<String> passengerIdList = new ArrayList()
                for (DataSnapshot passenger Snapshot: dataSnapshot.getChildren()) {
                    String passengerId = passenger.getKey()
                    passengerIdList.add(passengerId)
                }
                getAssignPassengerPickUpLocation();
            }else{
                passengerID = "";
                if(pickupMarker !=null){
                    pickupMarker.remove();
                }
                if(AssignPassengerPickupLocationListener!= null){
                AssignPassengerPickupLocationref.removeEventListener(AssignPassengerPickupLocationListener);
            }}
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

第二个

实际上,您必须保持位置与“目的地”相同的位置,而不是在其他子位置中查询。

但是为什么您需要同时让所有用户请求位置或在活动内部将所有用户请求位置包含在一个活动中?

有关更多信息:

读写列表

https://firebase.google.com/docs/database/android/lists-of-data

如何构造NoSQL

https://www.youtube.com/watch?v=v_hR4K4auoQ