Firebase 实时数据库更新功能

Firebase Realtime Database Update Function

提问人:Sam 提问时间:7/19/2023 最后编辑:Sam 更新时间:7/19/2023 访问量:28

问:

所以我正在做一个关于酒店预订系统的学校项目。问题出在使用 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 数据库中的数据

Java Android Firebase 函数 firebase-realtime-database

评论

1赞 Alex Mamo 7/19/2023
我不确定我是否理解。此代码中究竟有哪些内容不符合您的预期?告诉我们共享代码出了什么问题。你有什么错误吗?
1赞 Community 7/19/2023
请澄清您的具体问题或提供其他详细信息,以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。
0赞 Sam 7/19/2023
此代码不更新数据库中的数据。我认为这是因为updateBooking对象为空。但我不知道如何解决它
0赞 Frank van Puffelen 7/19/2023
我建议在本地调试代码,看看到底出了什么问题。如果在共享的代码的每一行上设置断点,请在调试器中运行代码,然后检查每行上每个变量的值,哪一行没有执行您期望的操作?
1赞 Frank van Puffelen 7/19/2023
如果你不熟悉在 Android 上进行调试,Android 文档中有一个关于调试应用的精彩页面,可以帮助你入门,包括有关使用断点检查变量的部分。

答: 暂无答案