Android 如何访问Firebase中的嵌套子值
我有以下数据库结构 我正在尝试将内容下所有“xxxxxxx”项的数据库/content/xxxxxxx/匹配项放入Android 如何访问Firebase中的嵌套子值,android,arrays,firebase,firebase-realtime-database,Android,Arrays,Firebase,Firebase Realtime Database,我有以下数据库结构 我正在尝试将内容下所有“xxxxxxx”项的数据库/content/xxxxxxx/匹配项放入数组列表,以便迭代数组列表,并获取匹配项的所有图像。例如,对于Avery-Fit Solid Pant,我想获得喇叭袖衬衫、高领条纹上衣等的图像,然后换成钟形袖连衣裙,也这样做。下面是我尝试过的,但不起作用 matchRef = FirebaseDatabase.getInstance().getReference().child("/content"); mat
数组列表
,以便迭代数组列表
,并获取匹配项的所有图像。例如,对于Avery-Fit Solid Pant,我想获得喇叭袖衬衫、高领条纹上衣等的图像,然后换成钟形袖连衣裙,也这样做。下面是我尝试过的,但不起作用
matchRef = FirebaseDatabase.getInstance().getReference().child("/content");
matchRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
SliderItems mvalue = dataSnapshot.getValue(SliderItems.class);
DataSnapshot contentSnapshot = dataSnapshot.child("/matches");
Iterable<DataSnapshot> matchSnapShot = contentSnapshot.getChildren();
for (DataSnapshot match : matchSnapShot){
SliderItems c = match.getValue(SliderItems.class);
matchImages.add(c);
}
startMatchRecyclerView();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
matchRef=FirebaseDatabase.getInstance().getReference().child(“/content”);
matchRef.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
SliderItems mvalue=dataSnapshot.getValue(SliderItems.class);
DataSnapshot contentSnapshot=DataSnapshot.child(“/matches”);
Iterable matchSnapShot=contentSnapshot.getChildren();
用于(数据快照匹配:匹配快照){
SliderItems c=match.getValue(SliderItems.class);
添加(c);
}
startMatchRecyclerView();
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
}
我很确定我的数据库结构不正确,并按如下方式对其进行了重组。我认为这是一个更合适的结构。我可以访问匹配项以填充
数组列表
。如何迭代每个条目的数组列表
,找到“true”的成员,然后从每个“true”成员的内容中获取图像?当您侦听/content
的值时,您会得到一个包含所有内容的快照。要获取单个内容项(具有匹配的
属性),需要循环快照的子项:
matchRef = FirebaseDatabase.getInstance().getReference().child("/content");
matchRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
SliderItems mvalue = itemSnapshot.getValue(SliderItems.class);
DataSnapshot contentSnapshot = itemSnapshot.child("/matches");
Iterable<DataSnapshot> matchSnapShot = contentSnapshot.getChildren();
for (DataSnapshot match : matchSnapShot){
SliderItems c = match.getValue(SliderItems.class);
matchImages.add(c);
}
}
startMatchRecyclerView();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
matchRef=FirebaseDatabase.getInstance().getReference().child(“/content”);
matchRef.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot itemSnapshot:DataSnapshot.getChildren()){
SliderItems MVValue=itemSnapshot.getValue(SliderItems.class);
DataSnapshot contentSnapshot=itemSnapshot.child(“/matches”);
Iterable matchSnapShot=contentSnapshot.getChildren();
用于(数据快照匹配:匹配快照){
SliderItems c=match.getValue(SliderItems.class);
添加(c);
}
}
startMatchRecyclerView();
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
抛出databaseError.toException();//不要忽略错误
}
});
或者,您可以使用
ChildEventListener
,在这种情况下,您可以摆脱我添加的for
循环,因为它的onchildaded
将在我的代码中触发我称之为itemsnashot
。谢谢@FrankvanPuffelen这工作得很好!