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

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

问题描述

System.out.println(ref.child("mostafa_farahat22@yahoo.com").child("_email"));

*我正在尝试获取 child 的值,但我一直都在获取该值的 URL如何在我尝试通过此代码时获取此 URL 的值,但它为我提供了我想要获取 _email 值的 URL.

*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.

推荐答案

您从错误的角度看待这个概念.在使用 ref.child("mostafa_farahat22@yahoo.com").child("_email") 时,您只是简单地指向数据库中的特定位置,仅此而已.如果您想检索该特定位置的数据,请考虑以下 2 种方式.

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天全站免登陆