问题
I'm using FirebaseRecyclerOptions
because I upgraded to the new FirebaseUI 3.0 version but now I cannot retrieve anything from the database. The same code used to work very well in the old FirebaseRecylcerAdapter
method. It seems like it doesn't even enter onBindViewHolder
.
Initializations
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView = findViewById(R.id.main_recycler);
recyclerView.hasFixedSize();
recyclerView.setLayoutManager(linearLayoutManager);
queryDatbase = mDatabase.child("chatlist").child(cUser);
queryDatbase.keepSynced(true);
Query query = queryDatbase.orderByChild("time");
query.keepSynced(true);
FirebaseRecyclerAdapter code
firebaseoptions = new FirebaseRecyclerOptions.Builder<ConvModel>().setQuery(query,ConvModel.class).build();
firebaseadapter = new FirebaseRecyclerAdapter<ConvModel, ChatlistHolder>(firebaseoptions) {
@Override
protected void onBindViewHolder(final ChatlistHolder holder, int position,final ConvModel model) {
final String each_user_id = firebaseadapter.getRef(position).getKey();
Toast.makeText(MainActivity.this, each_user_id, Toast.LENGTH_SHORT).show();
//region SET EACH USER IMAGE and USERNAME
assert each_user_id != null;
mDatabase.child("users").child(each_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String dp = dataSnapshot.child("dp").getValue(String.class);
final String name = dataSnapshot.child("name").getValue(String.class);
holder.setName(name);
holder.setDp(dp,getApplicationContext());
// region OPEN CHAT ACTIVITY
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//open other activity
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});//endregion
}
@Override
public ChatlistHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chatlist_single_item_layout,parent,false);
return new ChatlistHolder(v);
}
};
recyclerView.setAdapter(firebaseadapter);
回答1:
Since version 3.0 of FirebaseUI, you need to explicitly call startListening() & stopListening() on the adapter to instruct it to start/stop retrieving data from the database.
From the Upgrading to FirebaseUI 3.0 guide:
Adapter Lifecycle - in previous versions the adapters began listening immediately upon instantiation and had a
cleanup()
call to stop listening. In 3.x you must explicitly callstartListening()
andstopListening()
or pass aLifecycleOwner
to the options builder.
来源:https://stackoverflow.com/questions/50703205/cannot-retrieve-data-using-firebaserecycleroptions-after-upgrading-to-firebaseui