我的android项目中有firebase数据库,其中包含三个值:代码,电子邮件,日期。我正在使用switchcompat在firebase数据库中对当前学生进行计数。当检查切换时,数据将输入到Firebase数据库中,以检查电子邮件在特定日期是否唯一。如果取消选中该开关,则该日期的数据将被删除。但是我的问题是,当我第一次检查开关时,它工作正常。并且在第二次取消选中该开关之后,当我再次对其进行检查时,它会将数据输入到firebase中,而且即使仍在检查开关,它也会立即自动将其删除。我不明白代码中的问题在哪里。 我在下面提供用于SwitchCompat的代码。 这是代码:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
final String date = df.format(c);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
final String previousDate = df.format(cal.getTime());
final int[] i = {0};
if (switch1.isChecked()) {
Query equery = present.orderByChild("email").equalTo(email);
equery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
String code = "1";
PresentStu presentStu = new PresentStu(FirebaseAuth.getInstance().getCurrentUser().getEmail(), date, code);
String uploadId = present.push().getKey();
present.child(uploadId).setValue(presentStu);
} else {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String eemail = snapshot.child("email").getValue().toString();
String ddate = snapshot.child("date").getValue().toString();
if (eemail.equals(eemail) && ddate.equals(date)) {
i[0]++;
break;
}
}
// Toast.makeText(User.this,""+i[0]+"",Toast.LENGTH_SHORT).show();
if (i[0] < 1){
String code = "1";
PresentStu presentStu = new PresentStu(FirebaseAuth.getInstance().getCurrentUser().getEmail(), date, code);
String uploadId = present.push().getKey();
present.child(uploadId).setValue(presentStu);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
} else {
Query equery = present.orderByChild("email").equalTo(email);
equery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String eemail = snapshot.child("email").getValue().toString();
String ddate = snapshot.child("date").getValue().toString();
if (eemail.equals(eemail) && ddate.equals(previousDate)) {
present.child(dataSnapshot.getChildren().iterator().next().getKey()).child("code").setValue("0");
}
if (eemail.equals(eemail) && ddate.equals(date)) {
snapshot.getRef().removeValue();
break;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
});
请帮助我找出代码中的不足之处。
答案 0 :(得分:0)
由于我不知道您数据库的结构,因此不能简单地复制/粘贴此代码段;这是您自己的代码,其中已删除了事件监听器。
正确地获取快照的正确路径并直接进行更改,而无需等待数据更改。
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
final String date = df.format(c);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
final String previousDate = df.format(cal.getTime());
final int[] i = {0};
if (switch1.isChecked()) {
Query equery = present.orderByChild("email").equalTo(email);
// TODO Change this:
DataSnapshot dataSnapshot = equery.doSomethingToGettheSnapshot()
if (!dataSnapshot.exists()) {
String code = "1";
PresentStu presentStu = new PresentStu(FirebaseAuth.getInstance().getCurrentUser().getEmail(), date, code);
String uploadId = present.push().getKey();
present.child(uploadId).setValue(presentStu);
} else {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String eemail = snapshot.child("email").getValue().toString();
String ddate = snapshot.child("date").getValue().toString();
if (eemail.equals(eemail) && ddate.equals(date)) {
i[0]++;
break;
}
}
// Toast.makeText(User.this,""+i[0]+"",Toast.LENGTH_SHORT).show();
if (i[0] < 1){
String code = "1";
PresentStu presentStu = new PresentStu(FirebaseAuth.getInstance().getCurrentUser().getEmail(), date, code);
String uploadId = present.push().getKey();
present.child(uploadId).setValue(presentStu);
}
}
}
} else {
Query equery = present.orderByChild("email").equalTo(email);
// TODO Change this:
DataSnapshot dataSnapshot = equery.doSomethingToGettheSnapshot()
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String eemail = snapshot.child("email").getValue().toString();
String ddate = snapshot.child("date").getValue().toString();
if (eemail.equals(eemail) && ddate.equals(previousDate)) {
present.child(dataSnapshot.getChildren().iterator().next().getKey()).child("code").setValue("0");
}
if (eemail.equals(eemail) && ddate.equals(date)) {
snapshot.getRef().removeValue();
break;
}
}
}
}
}
});