如何将数据添加到Firebase实时数据库中的现有数据库?

编程入门 行业动态 更新时间:2024-10-24 10:19:28
本文介绍了如何将数据添加到Firebase实时数据库中的现有数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码:

lateadd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); String anahtar=databaseReference.getKey().toString(); final String kisiId = user.getUid().toString().trim(); final String kisiAd = user.getDisplayName().toString().trim(); final String yayinlananYorum = edituruneYorumyap.getText().toString(); List<Urun> urunListesi = new ArrayList<>(); Urun urunler = new Urun(kisiId, kisiAd, yayinlananYorum, urununAdi.getText().toString()); urunListesi.add(urunler); String key = databaseReference.push().getKey(); databaseReference.child(anahtar).child("urunYorum").setValue(urunler); } });

databaseReference.child(anahtar).child("urunYorum").setValue(urunler); ->我认为我需要在这里进行更改,但我不知道.

databaseReference.child(anahtar).child("urunYorum").setValue(urunler); ->i think i need a change here but i don't know.

我想将数据添加到孩子的先前参考中.

I want to add data to the child's pre-existing reference.

当我运行此编码时,它将创建一个新引用.不幸的是,它增加了新的记录. 我该如何解决?

when I run this encoding, it creates a new reference. Unfortunately, it adds a new record. How can I fix?

推荐答案

此行String key = databaseReference.push().getKey();创建一个新的唯一引用,然后将其写入其中.如果要写入现有参考,则需要知道该参考的键,通常是从用户单击的UI项中读取该键.

This line String key = databaseReference.push().getKey(); creates a new unique reference, that you then write to. If you want to write to an existing reference, you'll need to know the key of that reference, typically by reading it from the UI item that the user clicked on.

请注意,调用setValue()会替换该位置上的所有数据.如果要更新数据的子集或添加新属性,则必须调用较低级别的setValue()或调用updateChildren().

Note that calling setValue() replaces all data at the location. If you want to update a subset of the data, or add new properties, you will either have to call setValue() on a lower level, or call updateChildren().

例如:

databaseReference.child(anahtar).child("urunYorum/newProperty").setValue(urunler);

或者:

Map<String,Object> values = new HashMap<String,Object>(); values.put("kisiId", kisiId); values.put("kisiAd", kisiAd); databaseReference.child(anahtar).child("urunYorum").updateChildren(values);

更多推荐

如何将数据添加到Firebase实时数据库中的现有数据库?

本文发布于:2023-10-27 02:57:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   如何将   实时   数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!