简体   繁体   English

Android Studio如何从Firebase推送中获取ID

[英]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) (其中fichaPet是广告类,可从Firebase数据库推送到其他对象中)

My BBDD: 我的BBDD:

Users Firebase Database 用户Firebase数据库

I wanna get push's ID to write inside Ads from Users of FirebaseDatabase, and do something like this: 我想要从FirebaseDatabase的用户那里获取推送ID以便在Ads内编写广告,然后执行以下操作:

 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. 但不会从先前发布的广告中获取先前的ID。 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. 如果您再次使用push()方法,将生成一个新的ID,并且不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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