Home > Software engineering >  Android Studio - listing reviews from Firebase
Android Studio - listing reviews from Firebase

Time:09-06

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. 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. The following is my code for registering a review to 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?

CodePudding user response:

It seems that you are storing data with reference of userId every time, It's a reason It updates existing records.

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

CodePudding user response:

When you're using:

.setValue(review)

According to the official documentation of DocumentReference#set(Object data) method:

Overwrites the document referred to by this 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:

Updates fields in the document referred to by this DocumentReference.

  • Related