我遇到以下问题。 我有一个Firebase数据库,其中存储有关房间的信息。 只有在满足某些条件并且可以提供所要求的日期时,我才需要添加房间预订。我设法做到了,并保留了一个以“ no”开头的变量“ alreadyBooked”,但如果条件不正确,它将变为“ yes”。 但是,尽管我的if语句中有Logs并且可以访问,但我的变量永远不会成为yes。
private View.OnClickListener bookEvent() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
Long roomNumber=type.getSelectedItemId()+1;
room="Room"+roomNumber;
Log.d("Db room to update",room);
ref_rooms=database.getReference("Rooms");
req_ref_room=database.getReference("Rooms").child(room);
alreadyBooked="no";
req_ref_room.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dsp:dataSnapshot.getChildren())
{
String id = dsp.getKey();
DatabaseReference book_ref = FirebaseDatabase.getInstance().getReference("Rooms").child(room).child(id);
book_ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Book book = dataSnapshot.getValue(Book.class);
@SuppressLint("SimpleDateFormat") SimpleDateFormat smt = new SimpleDateFormat("dd/mm/yyyy");
try {
Date book_checkin_date = smt.parse(book.getCheckin());
Date book_checkout_date = smt.parse(book.getCheckout());
Date user_checkin_date = smt.parse(checkin.getText().toString());
Date user_chekout_date = smt.parse(checkout.getText().toString());
if (user_checkin_date.after(book_checkin_date) && user_checkin_date.before(book_checkout_date) ||
(user_chekout_date.after(book_checkin_date) && user_chekout_date.before(book_checkout_date)) ||
(user_checkin_date.before(book_checkin_date) && user_chekout_date.after(book_checkout_date)) ||
(user_checkin_date.before(book_checkin_date) && user_chekout_date.after(book_checkin_date)) ||
(user_checkin_date.before(book_checkout_date) && user_chekout_date.after(book_checkout_date))) {
alreadyBooked = "yes";
}
} catch (ParseException e) {
alreadyBooked = "yes";
e.printStackTrace();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
if(alreadyBooked.equals("no"))
{
Toast.makeText(getContext(),"Success",Toast.LENGTH_LONG).show();
ref_rooms.child(room).push().setValue(new Book(auth.getUid(),checkin.getText().toString(),checkout.getText().toString(),200));
}
else
{
Toast.makeText(getContext(),"Declined",Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
}
为什么“已经预订”始终为否?