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?
2
Answers
It seems that you are storing data with reference of userId every time, It’s a reason It updates existing records.
Try this –
And Fetch records according to userId
When you’re using:
According to the official documentation of DocumentReference#set(Object data) method:
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: