Retrieving Keys from Firebase Request

I am having trouble getting keys from requested results. This is my Firebase database:

{
  "Users" : {
    "7idb6ThWR8aqmnEHFao5GRCV1kI3" : {
      "dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
      "status" : "hi my name is erik",
      "uid" : "7idb6ThWR8aqmnEHFao5GRCV1kI3",
      "username" : "erik"
    }
  },
  "posts" : {
    "-KfsrGsY8TWb2wiMFtAq" : {
      "dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
      "image" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Post_Images%2Fcropped1354055061.jpg?alt=media&token=77fbc9ed-4356-43c1-b7bb-9563300a8b7b",
      "small_title" : "tes",
      "summary" : "tes",
      "title" : "tes",
      "uid" : "7idb6ThWR8aqmnEHFao5GRCV1kI3",
      "username" : "erik"
    }
  }
}

      

I am trying to get KfsrGsY8TWb2wiMFtAq

but using this code below

Query mThisUsersPosts;

public static final String TAG = "blah" ;

mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts");

mThisUsersPosts = mDatabasePosts.orderByChild("uid").equalTo(mCurrentUser);

mThisUsersPosts.addListenerForSingleValueEvent(new ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot) {
                      final String posts_key = dataSnapshot.getChildren().toString();
                      Log.d(TAG,posts_key);
                  }

                  @Override
                  public void onCancelled(DatabaseError databaseError) {

                  }
              });

      

I don't understand why, instead of getting the result, I get random callbacks like:

com.google.firebase.database.DataSnapshot$1@cfbcf64
com.google.firebase.database.DataSnapshot$1@961642a
com.google.firebase.database.DataSnapshot$1@6f0d191

      

Thank you very much in advance!

+3


source to share


3 answers


When you make a query against a Firebase database, there could potentially be multiple results. Thus, the snapshot contains a list of these results. Even if there is only one result, the snapshot will contain a list of one result. Thus, you need to iterate over the children of the returned snapshot to get the individual results.

mThisUsersPosts.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            System.out.println(child.getKey());
            System.out.println(child.child("title").getValue());
        }
    }

      

Please note that I am wondering if the data structure you choose is right for your application. If your main use case is for displaying messages to a specific user, consider modeling the data as such:



{
  "Users" : {
    "7idb6ThWR8aqmnEHFao5GRCV1kI3" : {
      "dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
      "status" : "hi my name is erik",
      "uid" : "7idb6ThWR8aqmnEHFao5GRCV1kI3",
      "username" : "erik"
    }
  },
  "posts" : {
    "7idb6ThWR8aqmnEHFao5GRCV1kI3": {
      "-KfsrGsY8TWb2wiMFtAq" : {
        "dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
        "image" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Post_Images%2Fcropped1354055061.jpg?alt=media&token=77fbc9ed-4356-43c1-b7bb-9563300a8b7b",
        "small_title" : "tes",
        "summary" : "tes",
        "title" : "tes",
        "username" : "erik"
      }
    }
  }
}

      

With this structure, you can get messages to the user with a much simpler / faster construction:

mThisUsersPosts = mDatabasePosts.child(mCurrentUser);

mThisUsersPosts.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            System.out.println(child.getKey());
            System.out.println(child.child("title").getValue());
        }
    }

      

+2


source


What you need is to iterate over the messages to get the key from the individual message

for(Datasnapshot snap : datasnapshot.getChildren()){
    String key = snap.getKey();
     Post post =snap.getValue(Post.class);
  }

      



thank

+4


source


What you need to do is:

//This assumes Post is the data type you're trying to get.
final Posts posts = dataSnapshot.getValue(Posts.class);

      

If you have multiple posts you can do:

for(Datasnapshot data : datasnapshot.getChildren()){
     Post post = data.getValue(Posts.class);
  }

      

From there, you can get everything you need. The good thing about Firebase is that it will build your model for you. So you can do things like posts.getPostTile()

and posts.getPostSummary()

. It is very convenient.

0


source







All Articles