5

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();
                }
            }
        });
    }
KENdi
  • 7,576
  • 2
  • 16
  • 31
The Employee 123
  • 494
  • 1
  • 14
  • 27

2 Answers2

4

jReportByUserDatabase.push().setValue(incidentReportUser)

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

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
Cadet
  • 376
  • 1
  • 9
0

See push documentation

Note that your all your reports will now be stored one level down under a unique key created by .push()

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).push();
        DatabaseReference newReport = jReportByUserDatabase.push();

        HashMap<String, String> incidentReportUser = new HashMap<>();
        incidentReportUser.put("date", userReportDate);
        incidentReportUser.put("time", userReportTime);
        incidentReportUser.put("location", userReportLocation);
        incidentReportUser.put("description", userReportDescription);

        newReport.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();
                }
            }
        });
    }
Peter
  • 1,006
  • 7
  • 15