提问人:Sam 提问时间:7/19/2023 最后编辑:Sam 更新时间:7/19/2023 访问量:28
Firebase 实时数据库更新功能
Firebase Realtime Database Update Function
问:
所以我正在做一个关于酒店预订系统的学校项目。问题出在使用 Firebase 实时数据库的更新功能上。它只是不更新数据。这是我从 ChatGPT 那里得到的代码。它显示更新成功的 toast,但在数据库中,它没有更改。
详细信息页面.java
private void updateBookingData() {
// Retrieve the updated data from the EditText fields
String name = nameUET.getText().toString().trim();
String phone = phoneUET.getText().toString().trim();
String email = emailUET.getText().toString().trim();
String checkIn = checkInUET.getText().toString().trim();
String checkOut = checkOutUET.getText().toString().trim();
// Check if any of the fields are empty
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(email)
|| TextUtils.isEmpty(checkIn) || TextUtils.isEmpty(checkOut)) {
Toast.makeText(this, "Please fill in all the fields", Toast.LENGTH_SHORT).show();
return;
}
// Inside updateBookingData() method in DetailsPage.java
if (booking == null || TextUtils.isEmpty(booking.getBookingId())) {
Toast.makeText(this, "Booking data not available", Toast.LENGTH_SHORT).show();
return;
}
// Get the user's UID from Firebase Authentication
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser == null) {
// User is not signed in; you may want to handle this case accordingly
Toast.makeText(this, "User not signed in", Toast.LENGTH_SHORT).show();
return;
}
String userId = currentUser.getUid();
// Check if the booking object is null or doesn't have a valid bookingId
if (booking == null || TextUtils.isEmpty(booking.getBookingId())) {
Toast.makeText(this, "Booking data not available", Toast.LENGTH_SHORT).show();
return;
}
// Create a new Booking object with the updated data
Booking updatedBooking = new Booking(userId, name, phone, email, checkIn, checkOut);
// Update the data in Firebase Realtime Database using the bookingId
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("bookings")
.child("users").child(userId).child(booking.getBookingId());
databaseReference.setValue(updatedBooking)
.addOnSuccessListener(aVoid -> {
try {
Log.wtf("BookingName: ", name);
}catch (Exception e) {
Toast.makeText(this, "Failed to delete booking data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "Booking updated successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, ConfirmPage.class);
startActivity(intent);
})
.addOnFailureListener(e -> {
Toast.makeText(this, "Failed to update booking data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
});
}
我需要它来更新 Firebase Realtime 数据库中的数据
答: 暂无答案
上一个:具有 void 返回类型且不采用输入参数的函数的数据类型
下一个:我哪里做错了?
评论