【问题标题】:How to insert data in the firebase如何在firebase中插入数据
【发布时间】:2022-01-18 04:13:28
【问题描述】:

所以我在我的 firebase android studio 中添加约会详细信息时遇到了问题。我希望用户将他们的约会详细信息存储在约会数据库中,并且我还想检查用户是否已经预订了日期和日期。但是,在用户按下确认按钮后,它并没有将约会详细信息存储在 Firebase 中,并且也没有向用户提示他们的约会已成功注册的对话框消息?有没有办法解决这个问题?代码如下:

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) {

        }
    });

【问题讨论】:

  • 您可以查看官方文档,这很可能会有所帮助:firebase.google.com/docs/database/android/read-and-write
  • 这里有很多可能出错的地方,所以我建议在每个重要的行上设置断点,在调试器中运行代码,并检查该行上的所有变量是否都具有您期望的值。如果不是,那是第一行没有,什么值与您的预期不同? --- 另外:不要忽略错误。 onCancelled 至少应该是:public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }

标签: java android firebase-realtime-database


【解决方案1】:

还要检查 firebase 规则,根据您的要求将读写设置为 true

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    相关资源
    最近更新 更多