简体   繁体   中英

Android Studio how to get id from push of Firebase

I've done this push:

 mDatabaseReference = FirebaseDatabase.getInstance().getReference();
 mDatabaseReference.child(TipoAnuncio).push().setValue(fichaPet);

(Where fichaPet is a ad's class which is pushed in other object from firebase database)

My BBDD:

Users Firebase Database

I wanna get push's ID to write inside Ads from Users of FirebaseDatabase, and do something like this:

 mDatabaseReference.child("Usuarios").child(userId).child("Ads").push().setValue(mDatabaseReference.child("Usuarios").child(userId).child("Ads").push().getKey());

but it doesn't get previously ID from previously ad published. Thanks!!! :)

To solve this, please use the following code:

mDatabaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference tipoAnuncioRef = mDatabaseReference.child(TipoAnuncio);
String key = tipoAnuncioRef.push().getKey(); //This is the value of your key
tipoAnuncioRef.child(key).setValue(fichaPet);

If you are using the push() methid again, a new id will be generated and there is no need for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM