我在座位上,我必须从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作为参数获取。 我在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,则会收到空对象引用异常。
我无法真正摆脱它。任何建议都会有所帮助。
答案 0 :(得分:1)
如果我是你,我会在FirebaseRecyclerAdapter
内移动onDataChange()
的声明,以便您可以轻松地将mMembersDatabase
传递给构造函数,然后像这样更改回调:
public interface MyCallback {
void onCallback(FirebaseRecyclerAdapter firebaseRecyclerAdapter);
}
使用以下代码:
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");
FirebaseRecyclerAdapter<Groupmates, GroupmatesViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter <Groupmates, GroupmatesViewHolder> (
Groupmates.class,
R.layout.users_single_layout,
GroupmatesViewHolder.class,
mMembersDatabase
) {
@Override
protected void populateViewHolder(final GroupmatesViewHolder groupmatesViewHolder, final Groupmates groupmates, final int position) {
//SOME LOGIC HERE
}
};
myCallback.onCallback(firebaseRecyclerAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
然后在你的onStart()中,使用:
readData(new MyCallback() {
@Override
public void onCallback(FirebaseRecyclerAdapter firebaseRecyclerAdapter) {
mGroupmatesList.setAdapter(firebaseRecyclerAdapter);
}
});
答案 1 :(得分:0)
经过一天的研究,我终于找到了解决方案。我要感谢 @Alex Mamo 提供正确的指导和指导。但是,不足以解决问题。这就是我回答自己问题的原因。
从FirebaseDatabaseReference
获取Callback
后,我的回收站视图仍未填充。因此,只需通过Callback
获取数据库参考,不就足以看到最终结果。在回答this question以及 @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);
}
}
});
这工作得很好。希望它也能帮助别人。