Hiy guys,
I have a fragment with a RecyclerView
in it. That RecyclerView
is populated by a Firebase 'DatabaseReference' object and on that reference I have added a ValueListener as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
Log.d(TAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_home, container, false);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Do something
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
ItemRecyclerViewAdapter adapter = new ItemRecyclerViewAdapter(activity, itemsDatabaseReference);
adapter.setHasStableIds(true);
// use a linear layout manager
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(),
DividerItemDecoration.VERTICAL_LIST);
recyclerView.addItemDecoration(itemDecoration);
recyclerView.setAdapter(adapter);
return view;
}
ItemRecyclerViewAdapter
extends FirebaseRecyclerAdapter
. The problem is that onDataChange
happens after onResume
so that at times the recycler is empty. What I can't understand is the fact that the recycler view is not always empty. Sometimes data are shown and sometimes not.
Thanks