Jagmaster
Jagmaster

Reputation: 107

Getting keys from Firebase query

I am having problem getting keys from queried results. This is my Firebase Database structure:

{
  "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 am getting random callbacks such as:

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

Many thanks beforehand!

Upvotes: 3

Views: 2960

Answers (3)

Frank van Puffelen
Frank van Puffelen

Reputation: 598678

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
    public void onDataChange(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
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            System.out.println(child.getKey());
            System.out.println(child.child("title").getValue());
        }
    }

Upvotes: 2

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

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.

Upvotes: 0

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

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

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

Thanks

Upvotes: 4

Related Questions