如何将数据添加到Firebase实时数据库中的现有数据库? [英] how to add data to the existing database in a firebase realtime database?

查看:143
本文介绍了如何将数据添加到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实时数据库中的现有数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆