检索子值-firebase- [英] Retrieving child value -firebase-

查看:192
本文介绍了检索子值-firebase-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的System.out.println(ref.child( mostafa_farahat22@yahoo.com)子( _邮件)); 

*我试图得到一个孩子的值,但是所有的时候我都得到了这个值的URL
如何获得这个URL的值,当我尝试通过这个代码,但它让我的URLi想要获得_email值。

解决方案

您从错误的角度看待这个概念。
在使用 ref.child(mostafa_farahat22@yahoo.com)。child(_ email)时,您只是简单地指向您的特定位置数据库等等。
如果你想检索那个地方的数据,可以考虑这两种方法。

首先,如果你只想检索一次数据,你可以做如下:

  DatabaseReference ref = FirebaseDatabase.getInstance()。getReference(); 
DatabaseReference mostafa = ref.child(Users)。child(mostafa_farahat22@yahoo.com)。child(_ email);

mostafa.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
String email = dataSnapshot.getValue(String.class) ;
//根据电子邮件做你想做的事

$ b @Override
public void onCancelled(DatabaseError databaseError){

}
});

或者您想要实时检索该值,并在与数据库同时使用它值改变了,所有在同一时间,只要值在改变,那么你使用这个:

pre $ mostafa.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
String email = dataSnapshot.getValue(String.class);

display.setText电子邮件);
}

@Override
public void onCancelled(DatabaseError databaseError){


});

请注意两种方法的区别。首先是只有一次检索,第二是检索数据,每当价值改变。

请记住,我张贴的代码只是模板和可能需要和他们一起玩。


System.out.println(ref.child("mostafa_farahat22@yahoo.com").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.

解决方案

You are looking at the concept from the wrong angle. While using the ref.child("mostafa_farahat22@yahoo.com").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.

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("mostafa_farahat22@yahoo.com").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.

这篇关于检索子值-firebase-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆