5

My problem is that I can't retrieve the random key generated by Firebase when saving data (I'm obliged to use that random key), for example

enter image description here

I tried to use dataSnapshot.getKey(), but it didn't work,any help will be highly appreciated. Thanks in advance

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
TeachMePls
  • 197
  • 1
  • 2
  • 11
  • 2
    could you attach the entire code you are trying? – adolfosrs Jul 05 '16 at 11:57
  • see this question : [DataSnapshot is missing a constructor with no arguments](http://stackoverflow.com/questions/38144597/datasnapshot-is-missing-a-constructor-with-no-arguments/38144987#38144987) – Janki Gadhiya Jul 05 '16 at 12:23

4 Answers4

6

According to your problem you could use this:

For Android:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("posts");
String key =  myRef.push().getKey();           //this returns the unique key generated by firebase

myRef.child(key).child("author").setValue("gracehop");//this creates the reqs key-value pair
myRef.child(key).child("title").setValue("Announcing COBOL");//this creates the reqs key-value pair

For Web Dev:

var messageListRef = firebase.database().ref('posts');
var newMessageRef = messageListRef.push();
newMessageRef.set({
 'author': 'gracehop',
 'title': 'Announcing COBOL'
});
Pramod Garg
  • 2,183
  • 1
  • 24
  • 29
  • 1
    Dear sir, may i know how to use "String key = myRef.push().getKey();" in php ? (i am trying to insert data by using php so) – Pisumathu Jan 08 '17 at 14:47
1

You can do this

String key = ref.push().getKey();
ref.child(key).setValue(post);

Hope this answers your question :)

Firebase - Save Data on Android

Wilik
  • 7,630
  • 3
  • 29
  • 35
0

If you want to retrieve random key then you should use addChildEventValueListerner because it gives u child of the posts node one by one.

 FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
 DatabaseReference mref = firebaseDatabase.getReference();
 mref.child("posts").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              if (dataSnapshot.hasChildren()) {
                 String value = dataSnapshot.getValue(String.class);
              }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

If you know any of the field's data, say you know the author's name then you can retrieve the key/keys(if multiple author has same name). It is describe here very clearly: https://stackoverflow.com/a/38232280/5129997

Community
  • 1
  • 1
THe_strOX
  • 687
  • 1
  • 5
  • 16