Android Firebase,只需获取一个子对象的数据 [英] Android Firebase, simply get one child object's data
问题描述
我找到了一些类似于 Firebase检索子Android 。所有的解决方案都建议使用ChildEventListener,但是我现在需要获取这些数据,而不是当它被移动,删除,更新等等。
我的数据作为字符串保存在https://.firebaseio.com/users//creation中。我觉得必须有一些简单的方法来访问,而不需要做太多,因为如果我将确切的URL复制到我的浏览器,我可以看到:'创建:2015/05/31 21:33:55'就在我的Firebase Forge仪表板中。
如果有人知道如何在没有监听器的情况下访问它, !
Firebase侦听器会触发初始数据和任何更改。 b
$ b
如果您想要同步集合中的数据,请使用 ChildEventListener
。如果你想同步一个对象,使用 ValueEventListener
。请注意,在这两种情况下,您都不会获取数据。你正在同步它,这意味着回调可能会被多次调用:初始数据和数据更新。
这包括在 Firebase的Android快速入门指南。相关的代码和报价:
$ $ $ $ p $ Fire $ $ c $ FirebaseRef.child(message)。addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
System.out.println(snapshot.getValue()); //打印你有数据吗?你会爱上Firebase
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
在上面的例子中,数据的状态,然后每当数据的值改变时再一次。
请花一点时间来完成这个快速入门。它不应该超过15分钟,它会让你从很多头部抓挠和问题中解救出来。 Firebase Android指南可能是一个很好的下一个目的地,特别是对于这个问题: https://firebase.google.com/docs/database/android/read-and-写
I have been looking for a way to get one child object's data in Android Firebase.
I have found things like Firebase retrieve child Android. All the solutions are suggesting using a "ChildEventListener", however I need to get this data at this moment, not when it is moved, deleted, updated, etcetera.
My data is kept in https://.firebaseio.com/users//creation as a string. I figure there must be some simple way to access that without needing to do too much, because if I copy the exact URL to my browser, I can see the: 'creation: "2015/05/31 21:33:55"' right there in my "Firebase Forge Dashboard".
If anyone knows how to access this without a listener for a change that would be appreciated!
Thank you!
Firebase listeners fire for both the initial data and any changes.
If you're looking to synchronize the data in a collection, use ChildEventListener
. If you're looking to synchronize a single object, use ValueEventListener
. Note that in both cases you're not "getting" the data. You're synchronizing it, which means that the callback may be invoked multiple times: for the initial data and whenever the data gets updated.
This is covered in Firebase's quickstart guide for Android. The relevant code and quote:
FirebaseRef.child("message").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println(snapshot.getValue()); //prints "Do you have data? You'll love Firebase."
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
In the example above, the value event will fire once for the initial state of the data, and then again every time the value of that data changes.
Please spend a few moments to go through that quick start. It shouldn't take more than 15 minutes and it will save you from a lot of head scratching and questions. The Firebase Android Guide is probably a good next destination, for this question specifically: https://firebase.google.com/docs/database/android/read-and-write
这篇关于Android Firebase,只需获取一个子对象的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!