英文:
Update Firebase database data
问题
我正在尝试更新我的数据库:
这是我的代码,但似乎无法更新数据库(在Firebase设置中设置了写入访问权限为true)
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("items").child("1").child("itemAvailability").setValue("Not Available");
Java类
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;
}
}
英文:
I am trying to update my databse:
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;
}
}
答案1
得分: 2
你可以使用 Map 数据结构来实现类似的操作。
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);
这将获取物品 1 的可用性并将其更新为“不可用”。
英文:
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
答案2
得分: 0
以下是Firebase实时数据库插入、更新和删除操作(CRUD)的代码示例:
//****更新
var database = firebase.database();
let userRef = database.ref('myroot/items/');
userRef.child("1").child("itemAvailability").update("Not Available").then().catch();
//****插入
var database = firebase.database();
let userRef = database.ref('myroot/items/');
userRef.child("1").child("itemAvailability").set("Not Available").then().catch();
//****删除
let itemId = 1;
let userRef = database.ref('myroot/items/' + itemId);
userRef.remove().then().catch();
英文:
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();
答案3
得分: 0
Sure, here's the translated code:
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", "不可用");
ref.updateChildren(updateMap);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Please note that I've only translated the code portion as you requested. If you have any more code you'd like translated or any questions, feel free to ask.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论