如何将数据库引用(来自回调)传递给FirebaseRecyclerAdapter

阿齐琼·霍尔马托夫(Azizjon Kholmatov)

我现在必须从Callback获取Firebase数据库参考。因为我能够读/写数据库,所以它工作得很好。

public void readData(final MyCallback myCallback) {
    mCurrentUserDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String university = dataSnapshot.child("university").getValue().toString();
            String groupid = dataSnapshot.child("groupid").getValue().toString();
            //Having acquired university and group ID, now we can get reference to group members list
            mMembersDatabase = FirebaseDatabase.getInstance().getReference().child("Universities").child(university).child("Groups").child(groupid).child("Members");
            myCallback.onCallback(mMembersDatabase);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}

然后,在我活动的onStart()方法中,创建FirebaseRecyclerAdapter。由于它需要4个参数:

  • 模型类
  • 资源布局文件
  • 视图持有人类
  • Firebase数据库参考

我需要将我从Callback获取Firebase数据库引用作为参数传递我在onStart()中尝试了以下方法,但是它不起作用。

    readData(new MyCallback() {
        @Override
        public void onCallback(DatabaseReference databaseReference) {


           FirebaseRecyclerAdapter<Groupmates, GroupmatesViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter <Groupmates, GroupmatesViewHolder> (
                    Groupmates.class,
                    R.layout.users_single_layout,
                    GroupmatesViewHolder.class,
                    databaseReference
            ) {
                @Override
                protected  void populateViewHolder(final GroupmatesViewHolder groupmatesViewHolder, final Groupmates groupmates, final int position) {
                                 //SOME LOGIC HERE
                }
            };
    //Finally, setting the ready adapter to the RecyclerView
    mGroupmatesList.setAdapter(firebaseRecyclerAdapter);

        }
    });

如果我尝试在回调之外创建FirebaseRecyclerAdapter,则会收到空对象引用异常。

我真的无法摆脱困境。任何的意见都将会有帮助。

阿齐琼·霍尔马托夫(Azizjon Kholmatov)

经过一天的研究,我终于找到了解决方案。我要感谢@Alex Mamo他提供的正确指导和指导。然而,这是不是足以解决问题。这就是为什么我要回答自己的问题。

FirebaseDatabaseReference从中获取Callback,我的回收站视图仍然没有被填充。因此,通过仅仅让数据库裁判Callback实力足够,看最后的结果。借助对这个问题的答案以及@Alex Mamo提供的建议和指导的结合,我得以解决了这个问题。

因此答案很简单:我只是打电话给我的Callback内部onCreateView()然后创建了我的FirebaseReyclerAdapter内部Callback Method创建适配器时,在将适配器设置为之前,RecyclerView需要添加AdapterDataObsever适配器。

firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                @Override
                public void onItemRangeInserted(int positionStart, int itemCount) {
                    super.onItemRangeInserted(positionStart, itemCount);
                    int groupmate_count = firebaseRecyclerAdapter.getItemCount();
                    int lastVisiblePosition = mLinearLayoutManager.findLastVisibleItemPosition();
                    // If the recycler view is initially being loaded or the
                    // user is at the bottom of the list, scroll to the bottom
                    // of the list to show the newly added message.
                    if (lastVisiblePosition == -1 ||  (positionStart >= (groupmate_count - 1) && lastVisiblePosition == (positionStart - 1))) {
                        mGroupmatesList.scrollToPosition(positionStart);
                    }
                }
            });

这工作得很好。希望它也能帮助其他人。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章