我的firebase database
"tests": {
"$user_id": {
".read": "$user_id===auth.uid",
".write": "$user_id===auth.uid",
"$testId": {
"duration": {
".validate": "newData.val().length>1"
},
"date": {
".validate": "newData.val().length>1"
},
"status": {
".validate": "newData.val()==='Pending' || newData.val()==='Completed'"
}
}
}
},
基本上tests
表格有2个状态pending
和completed
,用户在应用程序中将有2个按钮用于test
的各自状态。
我在FirebaseRecyclerAdapter
下面列出了数据。
final DatabaseReference testsRef = FirebaseDatabase.getInstance().getReference(Constants.TEST_TABLE).child(FirebaseAuth.getInstance().getCurrentUser().getUid());
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter <Tests, TestListHolder > (
Tests.class,
R.layout.test_list_card,
TestListHolder.class,
testsRef
) {
@Override
protected void populateViewHolder(final TestListHolder viewHolder, final Tests model, final int position) {
testsRef.child(adapter.getRef(position).getKey()).orderByChild("status")
.equalTo(viewType).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//fill the view data
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
因此,只要按Pending
或Completed
按钮,我就会将viewType
值分别作为Pending
/ Completed
传递。但上述orderByChild
并不起作用,每次所有数据都将在两个视图中获取,我相信不会获取正确的数据库引用。有人可以帮助我实现这个目标吗?
以下是有人坚持的示例数据:
"jxW41BgS12edasWtUkZA2G0eLhf2" : { //logged in user id
"-KfFFioKvMDlAyqw43Yb" : { //testid
"duration" : 4,
"date" : 1489552731888,
"status" : "Completed"
},
"-KfFGqIdenjwNdfs232EQ" : {//testid
"duration" : 5,
"date" : 1489552731888,
"status" : "Pending",
}
}
答案 0 :(得分:1)
仅显示待处理的测试:
DatabaseReference testsRef = FirebaseDatabase.getInstance().getReference(Constants.TEST_TABLE).child(FirebaseAuth.getInstance().getCurrentUser().getUid());
Query pendingTestsQuery = testsRef.orderByChild("status").equalTo(viewType);
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter <Tests, TestListHolder > (
Tests.class,
R.layout.test_list_card,
TestListHolder.class,
pendingTestsQuery
) {
@Override
protected void populateViewHolder(final TestListHolder viewHolder, final Tests model, final int position) {
// TODO: display model in the viewHolder
}
};