TheStudent123 TheStudent123 - 24 days ago 6
Android Question

Android - Firebase - Save new Data without overwriting old Data

This Question was created as my previous question contained 2 question instead of narrowing it down to 1

Aim

Users will be able to store new data without overwriting their previously submitted data

Description

Currently, the User's Incident Report data within the Incident Report node will be overwritten when the User enters a new Report.

The Data from the old Incident Report sent by the user should be kept along with the new data.

This way the authorities will be able to view the previous reports and also the new report data.

Problem

Everytime a the currently signed in user saves a "Report", the New report data will overwrite the Old report data

Codes for Saving 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();
}
}
});
}

Answer Source

jReportByUserDatabase.push().setValue(incidentReportUser)

Write it this way (push() adds values instead of overriding).