DataSnapshot.getValue(valueType) with key

3,245 views
Skip to first unread message

Andrey

unread,
Mar 17, 2016, 6:25:37 AM3/17/16
to Firebase Google Group
Hello!

My question relates to this example from the docs:

// Get a reference to our posts
 
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");


 
// Attach an listener to read the data at our posts reference


 
ref.addValueEventListener(new ValueEventListener() {
     
@Override
     
public void onDataChange(DataSnapshot snapshot) {
         
System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
         
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
           
BlogPost post = postSnapshot.getValue(BlogPost.class);
           
System.out.println(post.getAuthor() + " - " + post.getTitle());
         
}
     
}


     
@Override
     
public void onCancelled(FirebaseError firebaseError) {
         
System.out.println("The read failed: " + firebaseError.getMessage());
     
}
 
});



I would like to know, is there a possibility to retrieve not only values by DataSnapshot.getValue(valueType) method, but also a key name?
I need it to to go from list to details view, for example, or perform data modification in specific item(blog post in example).

Thanks!

Jacob Wenger

unread,
Mar 17, 2016, 12:55:43 PM3/17/16
to fireba...@googlegroups.com
Yes, I think you are looking for DataSnapshot.getKey().

Cheers,
Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/c1a9ca5d-a57f-4324-b280-be642e659624%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted
Message has been deleted

Andrey

unread,
Mar 18, 2016, 1:17:30 AM3/18/16
to Firebase Google Group
Hello, Jacob!
Thanks for you reply! I know about DataSnapshot.getKey().
What I meant is that in the example BlogPost class looks like this :

public class BlogPost {
 
private String author;
 
private String title;

 
public BlogPost() {
   
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
 
}

 
public String getAuthor() {
   
return author;
 
}

 
public String getTitle() {
   
return title;
 
}
}



And I my version have an extra field - "key":

public class BlogPost {
 
private String key;
 
private String author;
 
private String title;

 
public BlogPost() {
   
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
 
}

 
public String getKey() {
   
return key;
 
}

 
public String getAuthor() {
   
return author;
 
}

 
public String getTitle() {
   
return title;
 
}
}



And i want to know is there a version of DataSnapshot.getValue() that can fill all fields including "key" as easy as in example:

BlogPost post = postSnapshot.getValue(BlogPost.class);


I understand that I can store key name in real "key" field and it will store it in child node like this:

{
    blogPosts
: [
           
"-KD2YN5tJNqsxcDhpXRP": {
                 key
: "-KD2YN5tJNqsxcDhpXRP",
                 author
: "Andrey",
                 title
: "Hello!"
           
}
   
]
}



but I just wander, if it can be achieved without data duplication?

Thanks!

Frank van Puffelen

unread,
Mar 18, 2016, 3:43:04 PM3/18/16
to Firebase Google Group
You could inject it with some well-placed Jackson annotiations.

But you should really wonder if this is the right approach to take. In general on Android I prefer to separate the value of an object with a reference to that object in the database. The value you get with snapshot.getValue(), the reference you get with snapshot.getRef().  If I want something that you can both get the value and the location from, I pass around the snapshot itself.

Andrey

unread,
Mar 21, 2016, 1:07:15 AM3/21/16
to Firebase Google Group
Thank you, Frank!

You are right, it is a better approach to pass  snapshot itself.

I was thinking about that all the weekends, and end up with an idea, that it would be awesome to have Java analogue of AngularFire synchronized object/array, so one can use it like this:


FirebaseObject<BlogPost>   post = new FirebaseObject<BlogPost>(firebaseRef);


post
.addValueEventListener(new ValueEventListener<BlogPost>() {
   
@Override
   
public void onDataChange(BlogPost value) {
       
System.out.println(value.title);

   
}
   
@Override
   
public void onCancelled(FirebaseError firebaseError) {
       
System.out.println("The read failed: " + firebaseError.getMessage());
   
}
});


post
.title = "new title";
post
.save();


Frank van Puffelen

unread,
Mar 21, 2016, 10:49:51 AM3/21/16
to Firebase Google Group
There is an equivalent to $firebaseArray in FirebaseUI called FirebaseListAdapter (or FirebaseRecyclerAdapter). Both build on a class called FirebaseArray, which handles the synchronization of database changes into the Android app.

There is currently no equivalent for $firebaseObject in FirebaseUI. We are intentionally skipping on three-way data binding for the moment.
Reply all
Reply to author
Forward
0 new messages