如何从android中的firebase获取孩子的价值? [英] How to get child of child value from firebase in android?
问题描述
如何获取ZNAME值?最初我需要比较关键(Ex :: Here ZONE_1),然后ZNAME需要得到。在此先感谢...
要访问数据库中的值,可以创建 DatabaseReference
为该位置。这里有三个引用到你的数据库中的位置:
$ $ $ $ $ c $ DatabaseReference zonesRef = FirebaseDatabase.getInstance()。getReference(ZONES);
DatabaseReference zone1Ref = zonesRef.child(ZONE_1);
DatabaseReference zone1NameRef = zone1Ref.child(ZNAME);
在这段代码中:
zonesRef
指向 / ZONES
zone1Ref
指向 / ZONES / ZONE_1
zone1NameRef
指向
/ ZONES / ZONE_1 / ZNAME
有关获取数据库参考的 Firebase文档以获取更多信息。
您可以将侦听器附加到每个引用,以获取该位置的值。例如,要获取 / ZONES / ZONE_1 / ZNAME
的值:
zone1NameRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.i(TAG,dataSnapshot.getValue(String.class);
$ b @Override
public void onCancelled(DatabaseError databaseError){
Log.w(TAG,onCancelled,databaseError.toException());
}
});
有关这种类型的读操作的更多信息, Firebase关于阅读值的文档。
如果您正在监听 / ZONES / ZONE_1
,您将得到 DataSnapshot
然后使用 DataSnapshot.child()
来获取 ZNAME
:
zone1Ref.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.i(TAG,dataSnapshot.child(ZNAME ).getValue(String.class);
$ b @Override
public void onCancelled(DatabaseError databaseError){
Log.w(TAG,onCancelled,databaseError.toException());
}
});
再上一层,您可以听取 / ZONES
,这将为您带来所有区域的快照。由于这处理了多个孩子,因此您需要通过 DataSnapshot.getChildren()
来遍历它们:
pre (DataSnapshot zoneSnapshot:dataSnapshot.getChildren()){
public void onDataChange(DataSnapshot dataSnapshot){
$ {$ b $>
Log.i(TAG,zoneSnapshot.child(ZNAME).getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError){
Log.w(TAG,onCancelled,databaseError.toException());
}
});
有关详情,请参阅。 最后,您可能想要查询以查找例如找到带有 要了解更多信息,请阅读有关排序和过滤数据的Firebase文档。 How to get ZNAME value? Initially i need to compare key(Ex::Here ZONE_1) and then ZNAME need to be get. Thanks in advance... To access a value in your database, you create a In this snippet: See theFirebase documentation on getting a database reference for more information. You can attach a listener to each of the references, to get the value at that location. For example, to get the value of the For more on this type of read operation, see the Firebase documentation on reading values. If you instead listen on One more level up, you can listen on For more on this, see the Firebase documentation on listening for lists of data. Finally, you might want to query to find a specific zone, for example to find the zone with To learn more about this, read the Firebase documentation on sorting and filtering data. 这篇关于如何从android中的firebase获取孩子的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
ZCOD的区域E:ECOR
:
Query zonesQuery = zonesRef.orderByChild(ZCODE)。 equalTo( ECOR);
zonesQuery.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot zoneSnapshot:dataSnapshot.getChildren()){
Log.i(TAG,zoneSnapshot.child(ZNAME).getValue(String.class);
}
}
@Override
public void onCancelled (DatabaseError databaseError){
Log.w(TAG,onCancelled,databaseError.toException());
}
});
DatabaseReference
for that location. Here are three references to locations in your database:DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("ZONES");
DatabaseReference zone1Ref = zonesRef.child("ZONE_1");
DatabaseReference zone1NameRef = zone1Ref.child("ZNAME");
zonesRef
points to /ZONES
zone1Ref
points to /ZONES/ZONE_1
zone1NameRef
points to /ZONES/ZONE_1/ZNAME
/ZONES/ZONE_1/ZNAME
:zone1NameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i(TAG, dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
/ZONES/ZONE_1
, you will get a DataSnapshot
of the entire node with all its properties. You then use DataSnapshot.child()
to get the ZNAME
from it:zone1Ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i(TAG, dataSnapshot.child("ZNAME").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
/ZONES
, which will get you a snapshot with all the zones. Since this handles multiple children, you will need to loop through them with DataSnapshot.getChildren()
:zonesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot zoneSnapshot: dataSnapshot.getChildren()) {
Log.i(TAG, zoneSnapshot.child("ZNAME").getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
"ZCODE": "ECOR"
:Query zonesQuery = zonesRef.orderByChild("ZCODE").equalTo("ECOR");
zonesQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot zoneSnapshot: dataSnapshot.getChildren()) {
Log.i(TAG, zoneSnapshot.child("ZNAME").getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});