如何准确地使用'addChildEventListener()'来达到以下目的? [英] How can I use 'addChildEventListener()' accurately for following purpose?
问题描述
我有一些数据存储在 FirebaseDatabase
中。
数据结构如下所示:
app
-ref
-requestID
-key
-key:value
-key:value
-key:value
以下是我试图检索数据使用 addChildEventListener()
:
$ b
ref = FirebaseDatabase.getInstance ).getReferenceFromUrl( https://app-q24sg.firebaseio.com/ref);
$ b $ ref.child(requestID).child(key).addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){
if(dataSnapshot.getValue()!= null){
Log.d(dataSnapshot,dataSnapshot.getValue()。toString());
} else {
}
}
...
...
});
我试过 我想要的是: 以便我可以做到这一点: 我知道我可以使用 那么,我该如何使用 如果你想得到像 但是你想用 I have some data stored in Here's what data-structure look like: Here's how I'm trying to retrieve data using The I tried What I want is: so that I can do this: I know I can use So, how can I use If you want the datas like But you want create
这篇关于如何准确地使用'addChildEventListener()'来达到以下目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ $ $ $ $ c $ D $ data $快速
$ D / dataSnapshot
D $ data $快速
Log.d(dataSnapshot,dataSnapshot.getKey());
但是我得到了:
$ b $ pre $ D $ data $快照键
D / dataSnapshot:key
D / dataSnapshot:key
{key = value,key = value,key = value}
final Map< String,String> map =(Map< String,String>)dataSnapshot.getValue();
key1 = map.get(key);
key2 = map.get(key);
addListenerForSingleValueEvent
或 addValueEventListener
来实现我想要的,但是我不想每次运行 onDataChange()
时都要运行这段代码而且我只能通过在 onChildAdded()
(或不是?)中添加代码来达到目的。
addChildEventListener()
来获得所需的结果呢?
{key = value,key = value,key = value}
这样的数据,只能得到。 child(requestID)
没有 .child(key)
with .addValueEventListener
。它返回整个节点。然后将其转换为散列图,((HashMap)dataSnapshot.getValue())
。通过((HashMap)dataSnapshot.getValue())。get(key)
获取具体值。
addChildEventListener()来创建
Hashmap
。在 addChildEventListener()
之前创建一个 Hashmap
,然后在 onChildAdded
,
final HashMap< String,Object> map = new HashMap<>();
ref.child(requestID).addChildEventListener(new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){$ b $ if(dataSnapshot.getValue() != null){
map.put(dataSnapshot.getKey(),dataSnapshot.getValue());
} else {
}
}
...
...
});
FirebaseDatabase
.app
-ref
-requestID
-key
-key: value
-key: value
-key: value
addChildEventListener()
: ref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://app-q24sg.firebaseio.com/ref");
ref.child(requestID).child(key).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Log.d("dataSnapshot", dataSnapshot.getValue().toString());
} else {
}
}
...
...
});
Log.d
here is printing out:D/dataSnapshot: value
D/dataSnapshot: value
D/dataSnapshot: value
Log.d("dataSnapshot", dataSnapshot.getKey());
but then I got:D/dataSnapshot: key
D/dataSnapshot: key
D/dataSnapshot: key
{key=value, key=value, key=value}
final Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
key1 = map.get("key");
key2 = map.get("key");
addListenerForSingleValueEvent
or addValueEventListener
for achieving what I want but I don't want the piece of code to run every time onDataChange()
is called and that I can only achieve by adding code in onChildAdded()
(or not?).addChildEventListener()
here to get the required results?{key=value, key=value, key=value}
, get only .child(requestID)
without .child(key)
with .addValueEventListener
. It returns whole node. Then cast it to a hashmap, ((HashMap) dataSnapshot.getValue())
. Get the specific value with ((HashMap) dataSnapshot.getValue()).get(key)
. Hashmap
with addChildEventListener()
. Create a Hashmap
before the addChildEventListener()
then fill it in onChildAdded
,final HashMap<String, Object> map = new HashMap<>();
ref.child(requestID).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
map.put(dataSnapshot.getKey(),dataSnapshot.getValue());
} else {
}
}
...
...
});