Skip to content Skip to sidebar Skip to footer

Getting Keys From Firebase Query

I am having problem getting keys from queried results. This is my Firebase Database structure: { 'Users' : { '7idb6ThWR8aqmnEHFao5GRCV1kI3' : { 'dPicture' : 'https://fi

Solution 1:

What you need is to iterate on posts to get key of individual post

for(Datasnapshot snap : datasnapshot.getChildren()){
    Stringkey= snap.getKey();
     Postpost=snap.getValue(Post.class);
  }

Thanks

Solution 2:

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. So you need to iterate over the children of the returned snapshot to get the individual result(s).

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

Note that I wonder if the data structure you picked is right for your app though. If your main use-case is to display the posts for 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 the posts for the user with the much simpler/faster construct:

mThisUsersPosts = mDatabasePosts.child(mCurrentUser);

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

Solution 3:

What you need to do is this:

//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 anything you need. This is the nice thing about Firebase, is that it will build your model for you. So you can do things like posts.getPostTile() and posts.getPostSummary(). It's really handy.

Post a Comment for "Getting Keys From Firebase Query"