此问题的创建是因为我之前的问题包含2个问题,而不是将其缩小为1
目标
用户将能够存储新数据而不会覆盖以前提交的数据
描述
目前,当用户输入新报告时,“事件报告”节点中的用户事件报告数据将被覆盖。
用户发送的旧事件报告中的数据应与新数据一起保存。
通过这种方式,当局将能够查看以前的报告以及新的报告数据。
问题
每当当前登录的用户保存"报告"时,新报告数据将覆盖旧报告数据
保存数据的代码
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();
}
}
});
}
答案 0 :(得分:2)
jReportByUserDatabase.push().setValue(incidentReportUser)
以这种方式编写(push()
添加值而不是覆盖)。
答案 1 :(得分:0)
请注意,现在,您的所有报告都会在 .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();
}
}
});
}