0

I am trying to update my databse:

enter image description here

This is my code, but it doesn't seem to update the database. (Write access is set to true in Firebase settings)

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
      ref.child("items").child("1").child("itemAvailability").setValue("Not Available");

Java class

public class Item {
    String itemName;
    String itemAvailability;

    public Item(String itemName, String itemAvailability) {
        this.itemName = itemName;
        this.itemAvailability = itemAvailability;
    }

    public Item() {
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemAvailability() {
        return itemAvailability;
    }

    public void setItemAvailability(String itemAvailability) {
        this.itemAvailability = itemAvailability;
    }
}
7
  • what is it doing if it is not updating? Commented Aug 27, 2020 at 22:31
  • @beastlyCoder no changes. I refreshed the firebase conosle, no changes Commented Aug 27, 2020 at 22:33
  • can you show the whole code you have for accessing the firebase content Commented Aug 27, 2020 at 22:34
  • @beastlyCoder added. Should I create a method and put the code inside it? Commented Aug 27, 2020 at 22:36
  • do you have a POJO class set up( with instance variables constructor etc) Commented Aug 27, 2020 at 22:38

3 Answers 3

2

You can do something like this with a Map Data structure.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

DatabaseReference itemRef = ref.child("items/1/itemAvailability");
Map<String, Object> itemUpdates = new HashMap<>();
itemUpdates.put("itemAvailability", "Not Available");

itemRef.updateChildrenAsync(itemUpdates);

This will take the item 1's availability and update it's availability to Not Available

Sign up to request clarification or add additional context in comments.

3 Comments

yes it was, i got it from this tutorial: firebase.google.com/docs/database/admin/…
i also made a typo in item, i changed it to the correct word "items"
@Efaz did this help?
0
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
      ref.child("items").child("1").addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
              HashMap<String, Object> updateMap = new HashMap<>();
              updateMap.put("itemAvailability", "Not Available");
              ref.updateChildren(updateMap);
              }

         @Override
              public void onCancelled(@NonNull DatabaseError error) {

             }
        });

Might work for you.

Comments

0

Here is the code for Firebase real-time database Insert, Update & Delete operation(CRUD).

//****UPDATE 
var database = firebase.database();
let userRef = database.ref('myroot/items/');
userRef.child("1").child("itemAvailability").update("Not Available").then().catch();

//****Insert 
var database = firebase.database();
let userRef = database.ref('myroot/items/');
userRef.child("1").child("itemAvailability").set("Not Available").then().catch();

//****Delete 
let itemId=1;
let userRef = database.ref('myroot/items/' + itemId);
userRef.remove().then().catch();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.