Java 检索子值 -firebase-
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38017765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving child value -firebase-
提问by Mohamed Farahat
System.out.println(ref.child("[email protected]").child("_email"));
*i`m trying to get a value of child but all time i get the URL of the value how to get the value of this URL as i try by this code but it get me the URLi want to get the _email value.
*我正在尝试获取 child 的值,但我一直在获取值的 URL 如何获取此 URL 的值,因为我尝试通过此代码但它让我获得了想要获取 _email 值的 URL。
采纳答案by Ramtin
You are looking at the concept from the wrong angle.
While using the ref.child("[email protected]").child("_email")
you are just simply pointing at a particular place in your database and nothing more.
If you want to retrieve the data in that particular place, consider these 2 ways.
你从错误的角度看待这个概念。使用 时,ref.child("[email protected]").child("_email")
您只是简单地指向数据库中的特定位置,仅此而已。如果您想在该特定位置检索数据,请考虑这两种方式。
First if you want to retrieve the data only once, you can do the following :
首先,如果您只想检索数据一次,您可以执行以下操作:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference mostafa = ref.child("Users").child("[email protected]").child("_email");
mostafa.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue(String.class);
//do what you want with the email
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
or maybe you want to retrieve the value in real time and use it in the same time that the database value is changed, all in the same time, whenever the value in changed, then you use this :
或者您可能想实时检索该值并在更改数据库值的同时使用它,所有这些都是在同一时间,每当值更改时,您就可以使用它:
mostafa.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.getValue(String.class);
display.setText(email);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Note the difference between the two methods. First is only for one time retrieve and the second is for retrieving the data whenever the value is changed.
注意这两种方法的区别。第一个仅用于一次检索,第二个用于在值更改时检索数据。
Just have in mind that the codes that i posted are just templates and may need to play with them a bit.
请记住,我发布的代码只是模板,可能需要稍微使用一下。
回答by adolfosrs
With ref.child("[email protected]").child("_email")
you are just setting the reference to the object. Take a look at the java firebase documentation to retrieve data.
随着ref.child("[email protected]").child("_email")
你是刚刚设置参照对象。查看java firebase 文档以检索数据。
To get the data you will need to set a listener for your reference
要获取数据,您需要设置一个侦听器以供参考
ref.child("[email protected]").child("_email").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// data available in snapshot.value()
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});