所以我的Firebase中有一些数据。它的结构是
(假设有更多数据) 我想获取banknoteType =" 100_dollar"和userId =" 1057 ..."
DatabaseReference database = FirebaseDatabase.getInstance().getReference("userBanknoteAmount");
Query firebaseQuery = database.orderByChild(1057...).equalTo(1057...);
firebaseQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//todo
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("onCancelledError", "loadPost:onCancelled", databaseError.toException());
}
});
但是onDataChange
事件并没有发生......我在误解什么?
编辑:我正在尝试调试它,onDataChange和onCancelled断点都没有停止
编辑2:我正在使用的当前代码是
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userBanknoteAmountRef = rootRef.child("userBanknoteAmount");
Query firebaseQuery = userBanknoteAmountRef.orderByChild("userId");//.equalTo(userId);
firebaseQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Log.d("test", "onDataChange: "+postSnapshot);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.wtf("onCancelledError", "loadPost:onCancelled", databaseError.toException());
}
});
答案 0 :(得分:0)
Firebase实时数据库不允许您链接多个过滤方法。如果您想获取以下内容的记录:userId="1057..."
,请更改以下代码行:
Query firebaseQuery = database.orderByChild(1057...).equalTo(1057...);
与
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userBanknoteAmountRef = rootRef.child("userBanknoteAmount");
Query firebaseQuery = userBanknoteAmountRef.orderByChild("userId").equalTo(1057...);
如果您希望获得banknoteType ="100_dollar"
和userId="1057..."
的所有记录,请参阅 post 中的答案。
根据您编辑的帖子,请同时更改以下代码:
Log.d("test", "onDataChange: "+postSnapshot);
到
Log.d("test", "onDataChange: "+postSnapshot.child("userId").getValue(String.class));