Android – Firebase – Save new data without overwriting old data… here is a solution to the problem.
Android – Firebase – Save new data without overwriting old data
This issue was created because my previous question contained 2 questions instead of narrowing it down to 1
Aim
Users will be able to store new data without overwriting previously submitted data
Description
Currently, when a user
enters a new report, the user event report data in the event reporting node is overwritten.
The data in the old event report sent by the user should be saved with the new data.
This will allow the authorities to view previous reports as well as new reported data.
Question
Each time the currently logged-on user saves a Report, the new report data overwrites the old report data
The code that holds the data
private void submitReport(final String userReportDate,final String userReportTime,
final String userReportLocation,final String userReportDescription) {
jReportCurrentUserID = FirebaseAuth.getInstance().getCurrentUser();
final String reportUserID = jReportCurrentUserID.getUid();
jReportByUserDatabase = FirebaseDatabase.getInstance().getReference().child("Incident Reports").child(reportUserID);
HashMap<String, String> incidentReportUser = new HashMap<>();
incidentReportUser.put("date", userReportDate);
incidentReportUser.put("time", userReportTime);
incidentReportUser.put("location", userReportLocation);
incidentReportUser.put("description", userReportDescription);
jReportByUserDatabase.setValue(incidentReportUser).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
jReportLoad.dismiss();
Toast.makeText(getActivity(), "Report was Sent", Toast.LENGTH_SHORT).show();
jReportDatePick.setText("");
jReportTimeEnt.setText("");
jReportLocationEnt.setText("");
jReportDescriptionEnt.setText("");
}else{
jReportLoad.dismiss();
Toast.makeText(getActivity(), "Report failed to be sent", Toast.LENGTH_SHORT).show();
}
}
});
}
Solution
jReportByUserDatabase.push().setValue(incidentReportUser)
Write like this (push()
to add the value instead of overwriting.