Skip to content Skip to sidebar Skip to footer

Get The Pushed Id For Specific Value In Firebase Android

I want to retrive the id that generated by firebase when I pushed value to it like next I want to retrieve '-KGdKiPSODz7JXzlgl9J' this id for that email I tried by getKey() but

Solution 1:

You can read the key from push() without pushing the values. Later you can create a child with that key and push the values for that key.

// read the index keyStringmGroupId= mGroupRef.push().getKey();
....
....
// create a child with index value
mGroupRef.child(mGroupId).setValue(newChatGroup());

mGroupId contains the key which is used to index the value you're about to save.

Solution 2:

UPDATE 1: it can obtain also by one line

Stringkey = mDatabase.child("posts").push().getKey();

//**************************************************************//

after searching and trying a lot of things i came to 2 ways to do that . 1. first one to get the key when i upload the post to the server via this function

publicvoiduploadPostToFirebase(Post post) {
      DatabaseReferencemFirebase= mFirebaseObject
            .getReference(Constants.ACTIVE_POSTS_KEY)
            .child(post.type);
      mFirebase.push().setValue(post);
      Log.d("Post Key" , mFirebase.getKey());
 }
  1. i used it in my code to get the key after i have already pushed it to node for it in my database

    publicvoidgetUserKey(String email) {
    
        Query queryRef = databaseRef.child(Constants.USERS_KEY)
            .orderByChild(Constants.USERS_EMAIL)
            .equalTo(email);
    
        queryRef.addChildEventListener(newChildEventListener() {
          @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated
          }
    
          @OverridepublicvoidonChildChanged(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated;
          }
    
          @OverridepublicvoidonChildRemoved(DataSnapshot dataSnapshot) {
            //TODO auto generated;
          }
    
          @OverridepublicvoidonChildMoved(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated
          }
    
          @OverridepublicvoidonCancelled(DatabaseError databaseError) {
            //TODO auto generated 
          }
      });
     }
    

Solution 3:

When you fire a Firebase query there can potentially be multiple results. So when you ask for the value of a query, Firebase returns a list of items. Even if there is only one matching item, it will be a list of one item.

So you will have to handle this list in your code:

users.orderByChild("email").equalTo("z@m.com").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            Log.d("User key", child.getKey());
            Log.d("User ref", child.getRef().toString());
            Log.d("User val", child.getValue().toString());
        }
    }

Solution 4:

In Java - Android Studio, you can get the unique pushed ID as the item is written to the db...

Per "Firebase's: Save Data on Android": You can use the reference to the new data returned by the push() method to get the value of the child's auto-generated key or set data for the child. Calling getKey() on a push() reference returns the value of the auto-generated key.

To get the reference at write time, instead of loading DATA with a single push()...

  • use push() to create a blank record in the database, return value is the record's reference.
  • use .getKey() to get the Key for that record.
  • use .setValue(DATA) to fill in the blank record

here's an example:

FirebaseDatabasefb_db_instance= FirebaseDatabase.getInstance(); 
    DatabaseReferencedb_ref_Main= fb_db_instance.getReference("string_db_Branch_Name");

    hashMap_record = newHashMap<String, String>();           //some random data 
    hashMap_record.put("key_Item1", "string_Item1");
    hashMap_record.put("key_Item2", "string_Item2");

    DatabaseReferenceblankRecordReference= db_ref_Main ;   

    DatabaseReferencedb_ref= blankRecordReference.push();   //creates blank record in dbStringstr_NEW_Records_Key= db_ref.getKey();             //the UniqueID/key you seek
    db_ref.setValue( hashMap_record);                         //sets the record 

Solution 5:

I solved it to get id you need to use firebase url

Stringuid= firebaseRef.child("users").push().getKey();
Log.i("uid", uid);

this will give you the key KG.....

Post a Comment for "Get The Pushed Id For Specific Value In Firebase Android"