Android 向Firebase添加新项目后的ListView复制

Android 向Firebase添加新项目后的ListView复制,android,firebase,listview,firebase-realtime-database,Android,Firebase,Listview,Firebase Realtime Database,我正在尝试将教师添加到firebase实时数据库,并在listView中检索教师,如下图所示: 但是,当添加一个新项目时,我两次检索到现有数据,一次检索到新数据,如下图所示: 此方法在ADMINHELPER中用于添加教师 private void addingteacher() { progressBar.setVisibility(View.VISIBLE); // opening the Custom dialog to get the information of

我正在尝试将教师添加到firebase实时数据库,并在listView中检索教师,如下图所示:

但是,当添加一个新项目时,我两次检索到现有数据,一次检索到新数据,如下图所示:

此方法在ADMINHELPER中用于添加教师

private void addingteacher() {
    progressBar.setVisibility(View.VISIBLE);

    // opening the Custom dialog to get the information of the new Teachers
    Dialog mydialog = new Dialog(teacher_admin.this);
    mydialog.setContentView(R.layout.addteachers);
    mydialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    progressBar.setVisibility(View.GONE);
    mydialog.show();
    mydialog.setOnDismissListener(dialog -> progressBar.setVisibility(View.GONE));


    // hooking all the views of the dialog

    EditText teache5rName = mydialog.findViewById(R.id.teacherssname_dialogbox);
    EditText registrationNumber = mydialog.findViewById(R.id.teachersReg_dialogbox);
    EditText passwordteacher = mydialog.findViewById(R.id.teacherspassword_dialogbox);
    EditText department = mydialog.findViewById(R.id.teachersDepartment_dialogbox);
    EditText mobilenumber = mydialog.findViewById(R.id.teacherMobilenumber_dialogbox);

    NeumorphButton addteachersButton = mydialog.findViewById(R.id.add_teachersDialog);


    // when the add button on the dialog is pressed
    addteachersButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // dilog and progress bar
            progressBar.setVisibility(View.VISIBLE);

            //converting the edittext the String Literals
            String tnamez = teache5rName.getEditableText().toString().trim();
            String tregNu = registrationNumber.getEditableText().toString().trim();
            String tpassw = passwordteacher.getEditableText().toString().trim();
            String tdepartment = department.getEditableText().toString().trim();
            String tmobileNum = "+91" + mobilenumber.getEditableText().toString().trim();

          // validating the fields wheather they are empty or not
            if (!validateFields(tnamez, tregNu, tpassw, tdepartment, tmobileNum)) {

                // if empty then show a error custom dialog
                donedialogboxMeathod(false);
                progressBar.setVisibility(View.GONE);
                return;
            }

            donedialogboxMeathod(true);

            // where the teachers are under so the adding teacher fuction is in admin so
           // adminhelper is created and called the function
            AdminHelper adminHelper = new AdminHelper();

            // adding the teacher
            adminHelper.addTEachers(tnamez, tregNu, tpassw, "No", tdepartment,
                    "No", tmobileNum, "No");

            // notifyin the adpater that we added new ber
            teacher_adapter.notifyDataSetChanged();

         // dissmissing the progressbar and Custom dialog
            progressBar.setVisibility(View.GONE);
            mydialog.dismiss();


        }
    });
public teachersHelper addteacher(String tName, String tregNumber, String tPassword,String 
                                 imageId, String tDepartment,String tEmail, String 
                                    tphoneNumber, String tadress) {

    // creating the teachers Helper Class
    teachersHelper teachersHelpers = new teachersHelper(tName, tregNumber, tPassword, imageId, tDepartment,
            tEmail, tphoneNumber, tadress);
    
    // database stuff
    DatabaseReference teacherreferenc = FirebaseDatabase.getInstance().
            getReference("Admin").child(mAuth.getUid()).child("INSTITUTE");
    
    //adding value
    teacherreferenc.child("TEACHERS").child(tregNumber).setValue(teachersHelpers);

    return teachersHelpers;

}
检索教师并将其添加到列表视图(我认为问题就在这里)

public void设置视图(){
//用于检索教师数据的Firebase变量
FirebaseAuth mAuth=FirebaseAuth.getInstance();
DatabaseReference DatabaseReference=FirebaseDatabase.getInstance()
.getReference(“Admin”).child(mAuth.getUid()).child(“INSTITUTE”)
.儿童(“教师”);
//教师与registrationNumber节点一起存储,因此我创建了一个arraylist
ArrayList RegistrationNumber=新的ArrayList();
//valueevent以重新修改数据
databasereference.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot snapshot){
//检查快照是否存在
if(snapshot.exists()){
//拿到所有的注册号
对于(DataSnapshot regis:snapshot.getChildren()){
//将注册号添加到arraylist
registrationnumber.add(regis.getKey());
}
//根据注册号的arraylist,我们将进行检索
//特定注册编号教师的数据
对于(int i=0;i
每次对
数据库引用进行更改时,您的
onDataChange
都会被调用,并在该路径上显示数据的完整快照。因此,即使只更改/添加/删除了一个子节点,
快照也会包含所有其他节点(未修改)已添加到
RegistrationNumber
的子节点

最简单的解决方案是清除
onDataChange顶部的
RegistrationNumber
public teachersHelper addteacher(String tName, String tregNumber, String tPassword,String imageId, String tDepartment,String tEmail, String tphoneNumber, String tadress) { // creating the teachers Helper Class teachersHelper teachersHelpers = new teachersHelper(tName, tregNumber, tPassword, imageId, tDepartment, tEmail, tphoneNumber, tadress); // database stuff DatabaseReference teacherreferenc = FirebaseDatabase.getInstance(). getReference("Admin").child(mAuth.getUid()).child("INSTITUTE"); //adding value teacherreferenc.child("TEACHERS").child(tregNumber).setValue(teachersHelpers); return teachersHelpers; }
public void settingViews(){

  // Firebase variables to retreive the data of the teacher
                FirebaseAuth mAuth = FirebaseAuth.getInstance();

                DatabaseReference databasereference = FirebaseDatabase.getInstance()
                        .getReference("Admin").child(mAuth.getUid()).child("INSTITUTE")
                        .child("TEACHERS");


                //the teachers are stored with the node of registrationNumber so i created a arraylist
                ArrayList<String> registrattionNumber = new ArrayList<>();


                // valueevent to retreving the data
                databasereference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {

                        // cheching if the snapshot exists
                        if (snapshot.exists()) {

                            // getting all the registration Number
                            for (DataSnapshot regis : snapshot.getChildren()) {

                                // adding the registrationNumbers to the arraylist
                                registrattionNumber.add(regis.getKey());

                            }

                            // based on the arraylist of the registrationNUmber we will be retriving
                            // the data of teachers of particular registrartion Number

                            for (int i = 0; i < registrattionNumber.size(); i++) {

                                //firebase stuff
                                Query teacherssdata = FirebaseDatabase.getInstance()
                                        .getReference("Admin").child(mAuth.getUid()).child("INSTITUTE")
                                        .child("TEACHERS").child(registrattionNumber.get(i));


                                // value evnet listener
                                teacherssdata.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot snapshot) {

                                        if (snapshot.exists()) {

                                          // retreiving all the teachers data from the snapshot
                                            String _name = snapshot.child("tName").getValue().toString();
                                            String _regno = snapshot.child("tregNumber").getValue().toString();
                                            String _pass = snapshot.child("tPassword").getValue().toString();
                                            String _img = snapshot.child("image").getValue().toString();
                                            String _depart = snapshot.child("tDepartment").getValue().toString();
                                            String _phonenumber = snapshot.child("tphoneNumber").getValue().toString();
                                            String _adress = snapshot.child("tadress").getValue().toString();
                                            String _emai = snapshot.child("tEmail").getValue().toString();

                                           // creating the teacherclass and adding all the info from the firebase
                                            teachersHelper teachersHelperzzzz = new
                                                    teachersHelper(_name, _regno, _pass, _img, _depart, _emai
                                                    , _phonenumber, _adress);

                                           // adding the teacher objects to the Golbal Variable
                                            teachersFromdatabase.add(teachersHelperzzzz);

                                            // creating listview
                                            // creating the adapter
                                            teacher_adapter = new teacher_adapter(teacher_admin.this,
                                                    teachersFromdatabase);

                                          // setting the adapter to the listview
                                            listView.setAdapter(teacher_adapter);
                                            progressBar.setVisibility(View.GONE);

                                           


                                        } else {
                                            progressBar.setVisibility(View.VISIBLE);
                                            StyleableToast.makeText(teacher_admin.this, "Deleted Sucessfully"
                                                    , R.style.exampleToast).show();

                                        }


                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError error) {
                                        dialogboxMeathod(teacher_admin.this, error.getMessage());
                                    }
                                });}
databasereference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        registrattionNumber.clear();
        ...