如何使用Firebase中的“ChildEventListener”从最高/最高级别节点检索值? [英] How to retrieve value from the highest/last level node using 'ChildEventListener' from Firebase?

查看:232
本文介绍了如何使用Firebase中的“ChildEventListener”从最高/最高级别节点检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个字符串存储在特定的引用下: mReference.child(rID).child(userID2)我想用 childEventListener ,因为我正在做一些任务,当这些字符串被删除,这是唯一可能的与 onChildRemoved 。



这是我试过的:

$ p $ m $ c $ mReference.child(rID ).child(userID2).addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){
Log.d(dataHEre,dataSnapshot.getValue ().toString());
}
}

问题是我无法使用在此处检索数据 dataHEre 正在注销:

  D / dataHEre:28.8419556 
D / dataHEre:78.779063
D / dataHEre:3
D / dataHEre:Railway Colony
D / dataHEre:Sleep
D / dataHEre:12:36 AM
D / dataHEre:超人

其中



所以,我想知道我可以如何使用和使用 ChildEventListener 来检索数据,然后将检索到的数据分配给各种字符串<?/ p >

解决方案

我认为你正在以错误的方式进行查询。 onChildAdded 方法正在检索每个孩子的特定值(userID2我想)。如果这是你想要的,那么只要使用 onValueEventListener()来检索userId2节点的整个dataSnapshot。

如果您只想检索一次数据,您应该使用 onSingleValueEventListener()。并且 OnChildEvent()用于检索您想要单独跟踪每个子项的数据列表。例如,如果您将 OnChildEventListener 附加到 mReference.child(rID),您将得到一个为每个userId添加OnChildAdded ,例如填充 RecyclerView 或者 ListView 能够单独更新每个项目和您的Firebase数据。



如果我没有错,您想要的只是获取 userId2 引用,在这种情况下,将一个 OnValueEventListener 附加到该引用,并且每次修改,删除或添加值时都会收到一个调用。

pre $ firebaseDatabaseRef.addValueEventListener(new ValueEventListener(){
@Override public void onDataChange(DataSnapshot dataSnapshot){
customPOJO customPOJO = dataSnapshot.getValue(YourCustomPOJO.class);
customPOJO.getName();
customPOJO.getEmail();
customPOJO.getfavoriteFood();
//和等等...

}

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


I have a several strings stored under specific reference: mReference.child(rID).child(userID2) which I want to retrieve using childEventListener as I am doing some task also when these string gets removed and that is only possible with onChildRemoved of 'ChildEventListener'.

Here's what I have tried:

mReference.child(rID).child(userID2).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Log.d("dataHEre", dataSnapshot.getValue().toString());
            }
}

The problem is that I'm unable to retrieve data using keys here as dataHEre is logging out:

D/dataHEre: 28.8419556
D/dataHEre: 78.779063
D/dataHEre: 3
D/dataHEre: Railway Colony
D/dataHEre: Sleep
D/dataHEre: 12:36 AM
D/dataHEre: Superman

which are values.

So, I want to know how can I retrieve data here using keys and using ChildEventListener and then assign the data retrieved to various strings?

解决方案

I think that you are doing your query in the wrong way. onChildAdded method is retrieving you each child of a specific value (userID2 I suppose). If that is what you want, then just use a onValueEventListener() to retrieve the whole dataSnapshot of your userId2 node each time that it changes.

If you want to retrieve the data just once, you should use onSingleValueEventListener(). And OnChildEvent() is used to retrieve lists of data where you want to track each of the child individually. For example if you attach an OnChildEventListener to mReference.child(rID) you will get a OnChildAdded for each userId, what is pretty usefull like for example fill a RecyclerView or a ListView being able to update each item individually together with your Firebase Data.

If i'm not wrong what you want is just get updates of your userId2 reference, in that case attach a OnValueEventListener to that reference and you will get a call each time a value is modified, deleted, or added.

 firebaseDatabaseRef.addValueEventListener(new ValueEventListener() {
     @Override public void onDataChange(DataSnapshot dataSnapshot) {
        customPOJO customPOJO = dataSnapshot.getValue(YourCustomPOJO.class);
        customPOJO.getName();
        customPOJO.getEmail();
        customPOJO.getfavoriteFood();
        //and so on....

     }

     @Override public void onCancelled(DatabaseError databaseError) {

     }
  });

这篇关于如何使用Firebase中的“ChildEventListener”从最高/最高级别节点检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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