有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有人能告诉我在这种情况下我错在哪里吗?

我试图限制,如果程序名已经存在,并且连接的值不存在,则推送连接的值,否则不执行任何操作,但仍然会将连接的值推送到数据库中

代码如下:

public void addProgramAndDepartmentName(String programName, String departmentName) {    
    final DatabaseReference rootRef;
    rootRef = FirebaseDatabase.getInstance().getReference();

    String programAndDepartmentName = programName + departmentName;

    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if ( !(snapshot.child("Program & Departments").child(programName).exists()) ) {    
                rootRef.child("Program & Departments").child(programName).push().child("Program Name").setValue(programAndDepartmentName).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        /*AddCourses addProgramNameToSpinnerInAddCourse = new AddCourses();
                        addProgramNameToSpinnerInAddCourse.getDataIntoSpinnerFromFirebase();
                        addProgramNameToSpinnerInAddCourse.spinnerArrayAdapter.notifyDataSetChanged();*/

                        if (!task.isSuccessful()) {    
                            Toast.makeText(AddPrograms.this, "Program and Department is not Added into Database.", Toast.LENGTH_SHORT).show();

                        }    
                        else {    
                            Toast.makeText(AddPrograms.this, "Program and Department name is added into Database if.", Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();    
                        }
                    }
                });
            }
            else if( (snapshot.child("Program & Departments").child(programName).exists() ) &&
                    ( !( snapshot.child("Program & Departments").child(programName).child("Program Name").child(programAndDepartmentName).exists() ) ) ) {
                rootRef.child("Program & Departments").child(programName).push().child("Program Name").setValue(programAndDepartmentName).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task)  {    
                        /*AddCourses addProgramNameToSpinnerInAddCourse = new AddCourses();
                        addProgramNameToSpinnerInAddCourse.getDataIntoSpinnerFromFirebase();
                        addProgramNameToSpinnerInAddCourse.spinnerArrayAdapter.notifyDataSetChanged();*/

                        if (!task.isSuccessful()) {

                            Toast.makeText(AddPrograms.this, "Program and Department is not Added into Database.", Toast.LENGTH_SHORT).show();

                        }    
                        else {    
                            Toast.makeText(AddPrograms.this, "Program and Department name is added into Database else if.", Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();    
                        }
                    }
                });
            }    
            else {    
                Toast.makeText(AddPrograms.this, "This " + programName + " already Exists.", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();    
            }

        }

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

但唯一能正常工作的条件是第一个条件,即第二个条件,并且不工作

这是我的JSON文件:

{
  "Program & Departments" : {
    "Bs" : {
      "-MV4Heye6EkKPMFlL9IX" : {
        "Program Name" : "Bscs"
      },
      "-MV4Hfy_MHv0CVCnlsRZ" : {
        "Program Name" : "Bscs"
      }
    }
  }
}

请告诉我哪里错了


共 (1) 个答案

  1. # 1 楼答案

    如果希望存在具有特定值的单个节点,请将该值设置为该节点的键:

    {
      "Program & Departments" : {
        "Bs" : {
          "Bscs": true
        }
      }
    }
    

    使用这种结构,路径/Program & Departments/Bs/Bscs保证只存在一次,因为JSON只能包含该路径一次

    然后,您可以使用单个事务检查路径是否存在,如果不存在,则创建路径:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ref = rootRef.child(programName).child(programAndDepartmentName);
    ref.runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            if (!mutableData.exists())
                mutableData.setValue(true);
            }
            return Transaction.success(mutableData)
        }
    
        @Override
        public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot currentData) {
            Log.d(TAG, "runTransaction:" + databaseError);
        }
    });