从Firebase删除数据时,它会自动重新输入该数据

时间:2019-05-04 13:56:05

标签: java android xml

我正在为我的大学的一个小型项目开发一个android应用程序,但我遇到了困难。当我从Firebase删除数据时,它会自动重新输入该数据。我认为用于删除数据的活动会在删除后自动调用或恢复,因为用于删除数据的确认活动是在删除后开始的。请帮助我...

[Here is my database

  

UserInformation.java

package com.example.helpcenter;

public class UserInformation{
    private String age, date, gender, patient_name, phone_number,doctor_name,time, department_name,feedback;

    public UserInformation() {
    }

    public UserInformation(String age, String date, String gender, String patient_name, String phone_number, String doctor_name, String time, String department_name,String feedback) {
        this.age = age;
        this.date = date;
        this.gender = gender;
        this.patient_name = patient_name;
        this.phone_number = phone_number;
        this.doctor_name = doctor_name;
        this.time = time;
        this.department_name = department_name;
        this.feedback = feedback;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPatient_name() {
        return patient_name;
    }

    public void setPatient_name(String patient_name) {
        this.patient_name = patient_name;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getDoctor_name() {
        return doctor_name;
    }

    public void setDoctor_name(String doctor_name) {
        this.doctor_name = doctor_name;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDepartment_name() {
        return department_name;
    }

    public void setDepartment_name(String department_name) {
        this.department_name = department_name;
    }

    public String getFeedback() {
        return feedback;
    }

    public void setFeedback(String feedback) {
        this.feedback = feedback;
    }
}
  

RecycleView_config.java   删除数据

image_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage("Cancel the appointment?");
                builder.setCancelable(true);
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mReference.child(key).removeValue();
                        //mDoctor.child(sendDoc).child(userID).child(key).removeValue();
                        mOnlyDoctor.child(sendDoc).child(key).removeValue();

                        Intent i = new Intent(mContext,Previous.class);
                        mContext.startActivity(i);
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }

        });
  

发送数据

String patientName = text_patient_name.getEditText().getText().toString();
        String phoneNumber = patientPhoneNumber.getEditText().getText().toString();
        String ageString = Integer.toString(age);
        String gender =  radioGenderButton.getText().toString();

        final UserInformation addInfo = new UserInformation();
        addInfo.setPatient_name(patientName);
        addInfo.setPhone_number(phoneNumber);
        addInfo.setGender(gender);
        addInfo.setAge(ageString);
        addInfo.setDepartment_name(deptName);
        addInfo.setDoctor_name(doctorName);
        addInfo.setDate(date);
        addInfo.setTime(timeSlot);

        final UserInformation addDate = new UserInformation();
        addDate.setTime(timeSlot);


final FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();

        final DatabaseReference myRefAppointment = firebaseDatabase.getReferenceFromUrl("https://help-center-f4570.firebaseio.com/").child("appointments");

        final DatabaseReference myRefOnlyDoctor = firebaseDatabase.getReferenceFromUrl("https://help-center-f4570.firebaseio.com/");


        myRefAppointment.child(userID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    for (DataSnapshot ds : dataSnapshot.getChildren()){
                        String checkDateAppoint = ds.getKey().toString();
                        if (checkDateAppoint.equals(date)){
                            //flagAppoint = 1;
                            progressDialog.dismiss();
                            //dialogBox("You can book only one appointment in a day");

                            DialogSheet errorAppointment = new DialogSheet(Form.this)
                                    .setTitle("Error!!!")
                                    .setMessage("\nYou can book only one appointment in a day...")
                                    .setColoredNavigationBar(true)
                                    .setPositiveButton("Ok", new DialogSheet.OnPositiveClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            startActivity(new Intent(Form.this,Welcome.class));
                                        }
                                    })
                                    .setNegativeButton(android.R.string.cancel, null);

                            errorAppointment.show();

                            break;
                        }
                        else {
                            myRefOnlyDoctor.child(doctorName).addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                    if (dataSnapshot.exists()){
                                        for (DataSnapshot ds : dataSnapshot.getChildren()){
                                            final String checkDate = ds.getKey().toString();
                                            if (checkDate.equals(date)){
                                                myRefOnlyDoctor.child(doctorName).child(checkDate).addValueEventListener(new ValueEventListener() {
                                                    @Override
                                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                                        for (DataSnapshot ds4 : dataSnapshot.getChildren()){
                                                            String checkTime = ds4.getKey().toString();
                                                            if (checkTime.equals(timeSlot)){
                                                                progressDialog.dismiss();


                                                                DialogSheet errorDoctor = new DialogSheet(Form.this)
                                                                        .setTitle("Error!!!")
                                                                        .setMessage("\nThis doctor has already appointed by an other patient...")
                                                                        .setColoredNavigationBar(true)
                                                                        .setPositiveButton("Ok", new DialogSheet.OnPositiveClickListener() {
                                                                            @Override
                                                                            public void onClick(View view) {
                                                                                 startActivity(new Intent(Form.this,Welcome.class));
                                                                            }
                                                                        })
                                                                        .setNegativeButton(android.R.string.cancel, null);

                                                                errorDoctor.show();


                                                                break;
                                                            }
                                                            else {

                                                                myRefAppointment.child(userID).child(date).setValue(addInfo);

                                                                myRefOnlyDoctor.child(doctorName).child(date).child(timeSlot).setValue(addDate);

                                                                progressDialog.dismiss();
                                                                Intent i = new Intent(Form.this, Confirmed.class);
                                                                startActivity(i);
                                                                break;
                                                            }
                                                        }

                                                    }
                                                    @Override
                                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                                    }
                                                });
                                            }
                                            else{
                                                //docAppoint = true;
                                                myRefAppointment.child(userID).child(date).setValue(addInfo);

                                                myRefOnlyDoctor.child(doctorName).child(date).child(timeSlot).setValue(addDate);

                                                progressDialog.dismiss();
                                                Intent i = new Intent(Form.this, Confirmed.class);
                                                startActivity(i);
                                                break;
                                            }
                                        }
                                    }
                                    else{
                                        //docAppoint = true;
                                        myRefAppointment.child(userID).child(date).setValue(addInfo);
                                                                        myRefOnlyDoctor.child(doctorName).child(date).child(timeSlot).setValue(addDate);

                                        progressDialog.dismiss();
                                        Intent i = new Intent(Form.this, Confirmed.class);
                                        startActivity(i);

                                    }
                                }

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

                                }
                            });

                            //appoint = true;
                        }
                    }
                }
                else{
                    myRefAppointment.child(userID).child(date).setValue(addInfo);

                    myRefOnlyDoctor.child(doctorName).child(date).child(timeSlot).setValue(addDate);

                    progressDialog.dismiss();
                    Intent i = new Intent(Form.this, Confirmed.class);
                    startActivity(i);
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

0 个答案:

没有答案