How to insert data in the firebase

So i have problem regarding adding the appointment details in my firebase android studio. I want the user to store their appointment details in appointment database and I also want to check if the user has booked the date and day. However , after the user has pressed the confirm button, it doesnt store the appointment details in the firebase and it also doesnt prompt the dialog message to the user indicating that their appointment has been registered successfully? Is there any way to solve this problem ? The code is shown as below :

private void addAppointment() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("idPatient", FirebaseAuth.getInstance().getCurrentUser().getUid());
        hashMap.put("idDoctor", idDoctor);
        hashMap.put("time", time.getText().toString());
        hashMap.put("date", day.getText().toString());
        hashMap.put("status", "On hold");


    DatabaseReference reference1 = FirebaseDatabase.getInstance().getReference("Appointment");
    reference1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot data : snapshot.getChildren()) {
                Appointment relationship = data.getValue(Appointment.class);
                if (relationship.getTime().equals(time.getText().toString()) && relationship.getDate().equals(day.getText().toString()) && relationship.getIdDoctor().equals(idDoctor)) {
                    new SweetAlertDialog(BookAppointmentActivity.this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("Oops...")
                            .setContentText("The date and time has been booked")
                            .show();

                }else if(snapshot.exists()){
                    reference.child("Appointment").push().setValue(hashMap);
                    new SweetAlertDialog(BookAppointmentActivity.this, SweetAlertDialog.SUCCESS_TYPE)
                            .setTitleText("Congratulations")
                            .setContentText("Your appointment is registered successfully")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    Intent intent = new Intent(BookAppointmentActivity.this, MainActivity.class);
                                    startActivity(intent);
                                }
                            })
                            .show();
                }

            }
        }

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

        }
    });


from Recent Questions - Stack Overflow https://ift.tt/3nvRKtm
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation