简体   繁体   English

未从 firebase 检索数据

[英]Data is not retrieving from the firebase

Why am i not getting data even though i have specifically pointed my reference to that child of that child为什么我没有得到数据,即使我已经明确指出我提到了那个孩子的那个孩子

My goals is to:我的目标是:

Firstly, check if the customer has booked the specific room within the RoomsToCustomer table and if its true go to the second check which is checking if the roomname is still exist within the Rooms table if its true then retrieve everything from there, but the problem is it doesnt seem to retrieve the data at all, and im not sure why.首先,检查客户是否已经预订了 RoomsToCustomer 表中的特定房间,以及它的真实 go 到第二个检查,即检查房间名称是否仍然存在于 Rooms 表中,如果它是真的,然后从那里检索所有内容,但问题是它似乎根本没有检索数据,我不知道为什么。

@Override
    protected void onBindViewHolder(@NonNull @NotNull CustomerBookedRoomAdapter.myViewHolder holder, int position, @NonNull @NotNull RoomDataForAdapter model) {

        roomnameunderCustomerBooked = model.getRoomname();
        dataReferenceForCUSTBOOKEDADPT = FirebaseDatabase.getInstance().getReference().child("Rooms");
        dataReferenceForROOMBOOKEDBOOLEAN = FirebaseDatabase.getInstance().getReference().child("RoomsToCustomer");

        dataReferenceForROOMBOOKEDBOOLEAN.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull @NotNull DataSnapshot snapshotter) {
                if(snapshotter.child(roomnameunderCustomerBooked).child(usernameFGS).equals(true)){
                    dataReferenceForCUSTBOOKEDADPT.addValueEventListener(new ValueEventListener() {
                        @Override
                        //model.getRoomname().equals(snapshot.child("roomname").exists())
                        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                            if(roomnameunderCustomerBooked.equals(snapshot.child(roomnameunderCustomerBooked).exists())){

                                String updatedRoomStatus = snapshot.child(roomnameunderCustomerBooked).child("roomstatus").getValue().toString();
                                String roomType = snapshot.child(roomnameunderCustomerBooked).child("roomtype").getValue().toString();
                                String roomCapacity = snapshot.child(roomnameunderCustomerBooked).child("roomcapacity").getValue().toString();
                                String roomName = snapshot.child(roomnameunderCustomerBooked).child("roomname").getValue().toString();
                                String roomPrice = snapshot.child(roomnameunderCustomerBooked).child("roomprice").getValue().toString();
                                String roomCharacteristics = snapshot.child(roomnameunderCustomerBooked).child("roomcharacteristics").getValue().toString();
                                String roomImage = snapshot.child(roomnameunderCustomerBooked).child("imageUrl").getValue().toString();


                                holder.roomName.setText(roomName);
                                holder.roomType.setText(roomType);
                                holder.roomCapacity.setText(roomCapacity);

                                if(updatedRoomStatus.equals("Available")){
                                    String  availaFormat = "Available      ";
                                    holder.roomStatus.setText(availaFormat);
                                    holder.roomStatus.setTextColor(Color.parseColor("#226600"));
                                }else if(updatedRoomStatus.equals("Unavailable")){
                                    String  unavailaFormat = "Unavailable   ";
                                    holder.roomStatus.setText(unavailaFormat);
                                    holder.roomStatus.setTextColor(Color.parseColor("#C91818"));
                                }else if(updatedRoomStatus.equals("Maintenance")){
                                    String  umFormat = "Under\nMaintenance";
                                    holder.roomStatus.setText(umFormat);
                                    holder.roomStatus.setTextColor(Color.parseColor("#CCAA00"));

                                    //Set room "Under Maintenance" word alignment in a proper way
                                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    params.setMargins(29,-28,0,0);
                                    holder.roomStatus.setLayoutParams(params);
                                }
                                else{
                                    holder.roomStatus.setText(updatedRoomStatus);
                                    holder.roomStatus.setTextColor(Color.parseColor("#a4c639 "));
                                }
                                holder.roomPrice.setText(roomPrice);
                                holder.roomCharacteristics.setText(roomCharacteristics);
                                Picasso.get().load(roomImage).fit().into(holder.roomImage);


                            }


                        }



                        @Override
                        public void onCancelled(@NonNull @NotNull DatabaseError error) {

                        }
                    });




                }else{

                }

            }

            @Override
            public void onCancelled(@NonNull @NotNull DatabaseError error) {

            }
        });

Please take a look at my JSON file:请查看我的 JSON 文件:

{
  "Customer" : {
    "johnny123" : {
      "email" : "johnny@gmail.com",
      "fullname" : "johnny",
      "password" : "Abcbc12345",
      "username" : "johnny123"
    },
    "tjing123" : {
      "email" : "jh@gmail.com",
      "fullname" : "JingHuan",
      "password" : "1",
      "username" : "tjing123"
    }
  },
  "CustomerBookedRooms" : {
    "johnny123" : {
      "CozyCone" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F7b07e1ba-dabb-43b4-872b-087df75f8df6%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253A277e0ed8-e053-48bb-8d04-3d720ba8264e%2FORIGINAL%2FNONE%2F691350677?alt=media&token=b043022c-a54a-4e2e-afa2-345d266c9c47",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "2 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "CozyCone",
        "roomprice" : "288.00",
        "roomstatus" : "Available",
        "roomtype" : "Master Room",
        "username" : "johnny123"
      },
      "TheVamps" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F60ea14ef-93bf-4c04-bb75-0a95731511dd%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253A9145b259-426c-447a-a6b0-53cbf5770ace%2FORIGINAL%2FNONE%2F558417666?alt=media&token=eb57f6e6-fd81-4d90-a485-de40885a4a43",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "2 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "TheVamps",
        "roomprice" : "499.00",
        "roomstatus" : "Available",
        "roomtype" : "Master Room",
        "username" : "johnny123"
      }
    },
    "tjing123" : {
      "CozyCone" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F7b07e1ba-dabb-43b4-872b-087df75f8df6%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253A277e0ed8-e053-48bb-8d04-3d720ba8264e%2FORIGINAL%2FNONE%2F691350677?alt=media&token=b043022c-a54a-4e2e-afa2-345d266c9c47",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "2 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "CozyCone",
        "roomprice" : "288.00",
        "roomstatus" : "Available",
        "roomtype" : "Master Room",
        "username" : "tjing123"
      },
      "TheVamps" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F60ea14ef-93bf-4c04-bb75-0a95731511dd%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253A9145b259-426c-447a-a6b0-53cbf5770ace%2FORIGINAL%2FNONE%2F558417666?alt=media&token=eb57f6e6-fd81-4d90-a485-de40885a4a43",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "2 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "TheVamps",
        "roomprice" : "499.00",
        "roomstatus" : "Available",
        "roomtype" : "Master Room",
        "username" : "tjing123"
      }
    }
  },
  "Rooms" : {
    "CozyCone" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F7b07e1ba-dabb-43b4-872b-087df75f8df6%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253A277e0ed8-e053-48bb-8d04-3d720ba8264e%2FORIGINAL%2FNONE%2F691350677?alt=media&token=b043022c-a54a-4e2e-afa2-345d266c9c47",
      "roombedcounts" : "One Bed",
      "roomcapacity" : "2 pax",
      "roomcharacteristics" : "Cozy",
      "roomcounts" : "115",
      "roomid" : "1234",
      "roomname" : "CozyCone",
      "roomprice" : "288.00",
      "roomstatus" : "Available",
      "roomtype" : "Master Room"
    },
    "TheVamps" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F60ea14ef-93bf-4c04-bb75-0a95731511dd%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253A9145b259-426c-447a-a6b0-53cbf5770ace%2FORIGINAL%2FNONE%2F558417666?alt=media&token=eb57f6e6-fd81-4d90-a485-de40885a4a43",
      "roombedcounts" : "One Bed",
      "roomcapacity" : "2 pax",
      "roomcharacteristics" : "Cozy",
      "roomcounts" : "29",
      "roomid" : "8888",
      "roomname" : "TheVamps",
      "roomprice" : "499.00",
      "roomstatus" : "Available",
      "roomtype" : "Master Room"
    }
  },
  "RoomsToCustomer" : {
    "CozyCone" : {
      "johnny123" : true,
      "tjing123" : true
    },
    "TheVamps" : {
      "johnny123" : true,
      "tjing123" : true
    }
  },
  "Staff" : {
    "tt" : {
      "email" : "raymondting521@gmail.com",
      "fullname" : "raymondting",
      "password" : "Abcbc12345",
      "username" : "tt"
    }
  }
}

There is way too much going on in your question, so I'm just going to point out the first few problems that I noticed.你的问题太多了,所以我只想指出我注意到的前几个问题。 For future questions, please follow the guidance in how to create a minimal, complete, verifiable example .对于未来的问题,请遵循如何创建一个最小的、完整的、可验证的示例的指导。


This code won't work:此代码不起作用:

if (snapshotter.child(roomnameunderCustomerBooked).child(usernameFGS).equals(true)) {

Since snapshotter is a DataSnapshot , so is snapshotter.child(roomnameunderCustomerBooked).child(usernameFGS) .由于snapshotterDataSnapshot ,因此snapshotter.child(roomnameunderCustomerBooked).child(usernameFGS)如此。 So you're comparing a DataSnapshot object with a literal true value, and that's never going to match.因此,您将DataSnapshot object 与文字true值进行比较,这永远不会匹配。

You'll want to instead compare the boolean value of the DataSnapshot with true , which you can do with:您需要将DataSnapshot的 booleantrue进行比较,您可以这样做:

bool value = snapshotter.child(roomnameunderCustomerBooked).child(usernameFGS).getValue(Boolean.class);
if (value.equals(true)) {

You're leaving onCancelled empty, which means that you're ignoring important error messages that may be raised.您将onCancelled留空,这意味着您忽略了可能引发的重要错误消息。 onCancelled should never be empty, and at its minimum should look something like: onCancelled永远不应为空,并且至少应类似于:

public void onCancelled(@NonNull DatabaseError databaseError) { 
    throw databaseError.toException(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM