问题
my code:
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); ->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?
回答1:
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.
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().
So for example:
databaseReference.child(anahtar).child("urunYorum/newProperty").setValue(urunler);
Or:
Map<String,Object> values = new HashMap<String,Object>();
values.put("kisiId", kisiId);
values.put("kisiAd", kisiAd);
databaseReference.child(anahtar).child("urunYorum").updateChildren(values);
来源:https://stackoverflow.com/questions/53769224/how-to-add-data-to-the-existing-database-in-a-firebase-realtime-database