[英]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.