Before update:
I want update the value of Plavix 75mg:”50″. When user give a new value my code add a new value with a new ID instead of modifying the previous one. Here is the result:
Please help me to fix this.
Here is my code.
private void updatePharmaceutical(String documentId, String newDose, String newQuantity) {
if (storeName != null) {
CollectionReference storeCollection = db.collection(storeName);
Map<String, Object> updates = new HashMap<>();
updates.put("dose", newDose);
updates.put("quantity", newQuantity);
storeCollection.document(documentId).update(updates)
.addOnSuccessListener(aVoid -> {
Toast.makeText(this, "Successfully updated.", Toast.LENGTH_SHORT).show();
updateRealtimeDatabase(documentId, newQuantity); // Update Realtime Database with new quantity
})
.addOnFailureListener(e -> Toast.makeText(this, "Error caught while updating document", Toast.LENGTH_SHORT).show());
}
}
private void updateRealtimeDatabase(String drugName, String newQuantity) {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null) {
String userId = currentUser.getUid();
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Pharmacists").child(userId).child("drugData");
// Update Realtime Database with new quantity for the corresponding drug
userRef.child(drugName).setValue(newQuantity)
.addOnSuccessListener(aVoid -> Log.d("ViewPharmaceuticals", "Realtime Database updated with new quantity"))
.addOnFailureListener(e -> Log.e("ViewPharmaceuticals", "Error updating Realtime Database", e));
}
}
I want to modify the same value instead of adding a new field. I tried so many ways to do this but every attempt was useless.
Recognized by Mobile Development Collective and Google Cloud Collective
New contributor
Pramuka Navodh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.