简体   繁体   English

谁能告诉我在这种情况下我哪里错了?

[英]Can anyone tell me where i am wrong in this condition?

I am trying to restrict that if the program name already exists and the concatenated value does not exist then push the concatenated value otherwise do nothing but still, my concatenated value is getting pushed into the database.我试图限制如果程序名称已经存在并且连接值不存在,则推送连接值,否则什么都不做,但我的连接值将被推入数据库。

Here is the code:这是代码:

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

but the only condition that is working correctly is is first one the condition after And is not working但唯一正常工作的条件是第一个条件是 And is not working

And here is my JSON file:这是我的 JSON 文件:

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

please guide me where I am wrong?请指导我哪里错了?

If you want a single node with a specific value to exist, make that value the key of that node:如果您希望存在具有特定值的单个节点,请将该值作为该节点的键:

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

With this structure, the path /Program & Departments/Bs/Bscs is guaranteed to exist only once, since the JSON can only contain that path only once.使用这种结构,路径/Program & Departments/Bs/Bscs保证只存在一次,因为 JSON 只能包含该路径一次。

You can then check if the path exists, and create it if not, with a single transaction:然后,您可以使用单个事务检查路径是否存在,如果不存在则创建它:

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);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用eclipse并收到此错误,有人可以告诉我我要去哪里了 - I am using eclipse and getting this error can someone tell me where I am going wrong 谁能告诉我我做错了什么,“将数据从一个片段发送到另一个片段”? - Can anyone tell where i am doing it wrong, “sending the data from fragment to fragment”? 谁能告诉我我在做什么错? -堆栈 - Can anyone tell me what I'm doing wrong? - Stacks 我的代码遇到了运行时错误,有人可以帮我找出我哪里错了 - i got Runtime Error for my code ,can anyone help me to find out where i am wrong 来自孩子 class 的测试用例没有运行,遇到错误谁能指导我哪里出错了 - Test cases from child class is not running, facing error can anyone please guide me where I am going wrong 当我尝试在Java中运行以下二进制搜索代码时,它将引发ArrayOutOfExcepetion。 谁能看看我告诉我哪里出了问题? - When i try to run the below Binary Search code in java, it throws ArrayOutOfExcepetion. Can any anyone have a look and tell me where I went wrong? 任何人都可以告诉我为什么我得到SQL * PLUS无效标识符错误? - Can anyone tell me why I am getting a SQL*PLUS invalid identifier error? 我有一个do while循环问题。 谁能告诉我为什么这不起作用? - I am having a do while loop issue. Can anyone tell me why this is not working? 谁能告诉我这段代码出了什么问题? - Can anyone tell me what's going wrong with this code? 谁能告诉我这个递归函数出了什么问题? - Can anyone tell me what's going wrong with this recursive function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM