2

I am Retrieving values from Firebase Database in 4 Edittexts and 1 Spinner.

  • Edittext -> fName
  • Edittext -> lName
  • Edittext -> dateOfBirth
  • Spinner -> carType
  • Edittext -> carNumber

All these values are displayed. But how to edit them and update under button clicked.?

Followed documentation, but no success.

Please look at this post. you will understand

Zafar Kurbonov
  • 2,077
  • 4
  • 19
  • 42

2 Answers2

1

Assuming that you have the same database structure as in this question please use the following code:

final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

yourButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        rootRef.child("Drivers").child(driverId).child("carNumber").setValue("newCarNumber");
    }
});

In which driverId is the id of the particular driver for which you want to make updates. For the other fields just change carNumber with cardType, dateOfBirth, name or surname.

If you want to update data for the last driver for example, use this line:

rootRef.child("Drivers").child("-Kv6q2beyS50VCxxX3M7").child("carNumber").setValue("newCarNumber");
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hello, i am also using this method but i am having an issue. The data uploads to firebase but when the same data is deleted the onclick method runs it self and upload what ever is present in the edit texts. Note: this happens when app is open in background or foreground. – Mohammad Maaz Aug 19 '20 at 05:10
  • @MohammadMaaz Without seeing the code that you are using, I cannot be much of a help. So please post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Aug 19 '20 at 08:09
  • I did a work around and now I am not having the problem. Before i was using 'valueEventListener' which was causing the button click to run itself when the data was changed. After some learning i found that 'addListenerForSingleValueEvent' will run 1 time only, and this solved my problem – Mohammad Maaz Aug 21 '20 at 05:12
0

Should be easy, i am not shure if i understand what you need.

public void editData(String childKey, String fName, String lName, String dateOfBirth, String carType, String carNumber) {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child(childKey);
    databaseReference.child("fName").setValue(fName);
    databaseReference.child("lName").setValue(lName);
    databaseReference.child("dateOfBirth").setValue(dateOfBirth);
    databaseReference.child("carType").setValue(carType);
    databaseReference.child("carNumber").setValue(carNumber);
}
Milos Lulic
  • 627
  • 5
  • 22