Firebase 条件列表数据检索安卓



我的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",
    }
  }

仅显示挂起的测试:

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
  }
};

相关内容

  • 没有找到相关文章

最新更新