我有以下数据库结构。
我正在尝试将内容下的所有'xxxxxxx'项目的数据库/内容/ xxxxxxx /匹配到Array list
,以便我可以迭代Array list
并获取项目的所有图像那场比赛。例如,对于Avery-Fit Solid Pant,我想获得带有喇叭袖,高领条纹上衣等的衬衫的图像,然后移动到Bell Sleeve Dress并做同样的事情。以下是我尝试过的,但它不起作用。
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) {
}
});
}
我很确定我的数据库结构不正确,并按如下方式对其进行了重组。 我认为这是一个更合适的结构。我可以访问匹配项以填充Array list
。如何为每个条目迭代Array list
以找到“真实”的成员,然后从每个“真实”成员的内容中获取图像?
答案 0 :(得分:0)
当您收听/content
的值时,您会获得包含其中所有内容的快照。要获取单个内容项(具有matches
属性),您需要遍历快照的子项:
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
}
});
或者您可以使用ChildEventListener
,在这种情况下,您可以摆脱我添加的for
循环,因为它的onChildAdded
将会触发我所谓的{{1}在我的代码中。