Firebase条件列表数据检索android

时间:2017-03-18 10:36:45

标签: android firebase firebase-realtime-database firebase-authentication firebaseui

我的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个状态pendingcompleted,用户在应用程序中将有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) {
      }
    });

  }
};

因此,只要按PendingCompleted按钮,我就会将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",
    }
  }

1 个答案:

答案 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

  }
};