简体   繁体   English

Android Studio - 列出来自 Firebase 的评论

[英]Android Studio - listing reviews from Firebase

I'm pretty new when it comes to Android Studio, but have been watching a lot of tutorials where I can register users, etc. But I now want a user to be able to leave a review that is stored in Firebase.当谈到 Android Studio 时,我还是个新手,但是我一直在看很多可以注册用户等的教程。但是我现在希望用户能够留下存储在 Firebase 中的评论。 I have created a RecyclerView so I can view the reviews left by all users, but I am having an issue where when I leave a review, it deletes the current review in Firebase, so there is always only one review.我创建了一个 RecyclerView,这样我就可以查看所有用户留下的评论,但是我遇到了一个问题,当我发表评论时,它会删除 Firebase 中的当前评论,所以总是只有一个评论。 The following is my code for registering a review to Firebase:以下是我注册评论到 Firebase 的代码:

public void registerReview(){
        String email = emailR.getText().toString().trim();
        String message = messageR.getText().toString().trim();


        Review review = new Review(email, message);
        FirebaseDatabase.getInstance().getReference("Review")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .setValue(review).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if(task.isSuccessful()){
                    Toast.makeText(RegisterReview.this, "Thanks for your feedback!", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(RegisterReview.this, MainMenu.class));
                }
                else{
                    Toast.makeText(RegisterReview.this, "Failed to Register", Toast.LENGTH_LONG).show();
                }
            }
        });

How would I change this so every review is stored without deleting the previous?我将如何更改这一点,以便在不删除以前的情况下存储每条评论? Is this occurring due to.setValue?这是由于.setValue 而发生的吗?

It seems that you are storing data with reference of userId every time, It's a reason It updates existing records.似乎您每次都在参考 userId 存储数据,这是它更新现有记录的原因。

Try this -尝试这个 -

    String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    String uniqueID = UUID.randomUUID().toString();

    Review review = new Review(userID ,email, message);
    FirebaseDatabase.getInstance().getReference("Review")
    .child(uniqueID)
            .setValue(review).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                Toast.makeText(RegisterReview.this, "Thanks for your feedback!", Toast.LENGTH_LONG).show();
                startActivity(new Intent(RegisterReview.this, MainMenu.class));
            }
            else{
                Toast.makeText(RegisterReview.this, "Failed to Register", Toast.LENGTH_LONG).show();
            }
        }
    });

And Fetch records according to userId并根据userId获取记录

When you're using:当您使用时:

.setValue(review)

According to the official documentation of DocumentReference#set(Object data) method:根据DocumentReference#set(Object data)方法的官方文档:

Overwrites the document referred to by this DocumentReference.覆盖此 DocumentReference 引用的文档。

So that's the reason why, if you use the same reference, you're overwriting the data.这就是为什么如果您使用相同的引用,您将覆盖数据。 If you don't want that, then you should consider using DocumentReference#update(Map<String, Object> data) which:如果您不希望这样,那么您应该考虑使用DocumentReference#update(Map<String, Object> data)

Updates fields in the document referred to by this DocumentReference.更新此 DocumentReference 引用的文档中的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM