简体   繁体   中英

How to get random key from firebase realtime database in android?

When i push to create a random generated entry i want to save the randomly generated value as a field inside of itself.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMapsBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());


    manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    reference = FirebaseDatabase.getInstance().getReference().child("Guest").push();


    getLocationUpdates();
    readChanges();
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

Firebase 屏幕截图

Calling push() generates a new DatabaseReference with a unique key. You can get that key with:

reference = FirebaseDatabase.getInstance().getReference().child("Guest").push();
String key = reference.getKey();

So you can then write that value into the database where you need it.

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