我正在尝试使用查询填充ListView,该查询将检索布尔值等于false的所有GroupVaccination,即allVaccinated = false。数据库的结构如下:allVaccinated is the condition I'm looking to use
以下代码是我尝试过的,但是没有呈现任何内容:
databaseReferenceGV = FirebaseDatabase.getInstance().getReference("groupVaccinations");
groupVaccinationList = new ArrayList<>();
groupVaccinationList.clear();
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_vaccinations:
listView.setAdapter(null);
groupVaccinationList.clear();
Query toDoVaccinationQuery = databaseReferenceGV.orderByChild("groupVaccinationID/allVaccinated").equalTo(false);
toDoVaccinationQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot groupVaccinationSnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot vaccinationSnapshot : groupVaccinationSnapshot.getChildren()) {
GroupVaccination groupVaccination = vaccinationSnapshot.getValue(GroupVaccination.class);
groupVaccinationList.add(groupVaccination);
}
}
AllGroupVaccinationList groupVaccinationInfoAdapter = new AllGroupVaccinationList(ActivityToDoDoses.this, groupVaccinationList);
listView.setAdapter(groupVaccinationInfoAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
return true;
case R.id.navigation_doses:
//other action
}
return false;
}
};
我不太确定如何查询Firebase数据库,因此欢迎您提供任何帮助!谢谢!
答案 0 :(得分:0)
您不能使用以下查询:
databaseReferenceGV.orderByChild("groupVaccinationID/allVaccinated").equalTo(false);
由于在groupVaccinationID
节点和allVaccinated
属性之间,还有两个子节点:-LXiM ... BX9x
和-LZk5 ... j8Vz
。为了解决这个问题,您可以使用DataSnapshot
方法对getChildren()
对象进行两次迭代(就像您已经做过的那样),但是在这种情况下,只需要以下查询:
databaseReferenceGV = FirebaseDatabase.getInstance().getReference("groupVaccinations");
然后创建一个if语句客户端:
if(!groupVaccination.getAllVaccinated()) {
groupVaccinationList.add(groupVaccination);
}
或者您可以更改数据库结构以仅允许基于单个动态节点进行查询。