简体   繁体   中英

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.

@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:

{
  "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) . So you're comparing a DataSnapshot object with a literal true value, and that's never going to match.

You'll want to instead compare the boolean value of the DataSnapshot with true , which you can do with:

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 should never be empty, and at its minimum should look something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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