英文:
Firebase Realtime Database - Updating Existing Value
问题
我是新手使用Firebase,需要一些帮助。
我想要将“Cart”值插入到“MCwD6C_-XCZNGWxFLhO”用户中,“Cart”具有“itemName”、“quantity”和“itemPrice”字段。
我尝试了很多方法,但仍然无法做到。
这是我的当前Firebase结构
{
-User
-MCwD6C_-XCZNGWxFLhO
email:testing@gmail.com
username:testing
}
我想要插入新的数据,使其变成这样
{
-User
-MCwD6C_-XCZNGWxFLhO
email:testing@gmail.com
username:testing
-Cart
itemName:a
itemPrice:66
}
英文:
I'm new to Firebase and I need some help.
I want to insert "Cart" value inside "MCwD6C_-XCZNGWxFLhO" user, "Cart" has "itemName","quantity","itemPrice" fields.
I tried a lot of ways still can't do it.
this is my current firebase structure
{
-User
-MCwD6C_-XCZNGWxFLhO
email:testing@gmail.com
username:testing
}
I want to insert new data become like this
{
-User
-MCwD6C_-XCZNGWxFLhO
email:testing@gmail.com
username:testing
-Cart
itemName:a
itemPrice:66
}
答案1
得分: 1
要更新数据,您需要获取key
。
获取key
的方法如下:
String key;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
.child("User");
Query query = databaseReference.orderByChild("username").equalTo("testing");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
key = snapShot.getKey(); //Key
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
要添加数据,可以使用以下代码:
HashMap<String, Object> map = new HashMap<>();
map.put("itemName", "a");
map.put("itemPrice", "66");
databaseReference.child(key).child("Cart").updateChildren(map);
英文:
You need the key
to update data,
To get key:
String key;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
.child("User");
Query query = databaseReference.orderByChild("username").equalTo("testing");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
key = snapShot.getKey(); //Key
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
To add date
HashMap<String, Object> map = new HashMap<>();
map.put("itemName", "a");
map.put("itemPrice", "66");
databaseReference.child(key).child("Cart").updateChildren(map);
答案2
得分: 0
如果您已保存了密钥-MCwD6C_-XCZNGWxFLhO
,您可以使用setValue(newValue)
来更新该值。有关更多信息,请查阅此参考文档。例如,假设您已执行了mDatabase.child("User").child(yourKey).setValue(yourObject);
,其中yourObject没有Cart对象或Cart对象为空。当您的Object中已经准备好Cart对象并希望推送该值时,再次执行相同的操作,这次您的Firebase数据将会更新。
英文:
If you have the key -MCwD6C_-XCZNGWxFLhO
saved, you can just update the value using setValue(newValue)
. Check this reference for more. For example, let's say you have done mDatabase.child("User").child(yourKey).setValue(yourObject);
where yourObject has no Cart object or the Cart object is null. When you have the Cart object ready in yourObject and you want to push the value, do the same thing again and this time your firebase data will be updated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论