populateViewHolder() Not Being Called; RecyclerView Not Loading
See original GitHub issueI have scoured StackOverflow and this forum to find an answer, but for whatever reason my RecyclerView is not populating data with the FIrebaseRecyclerAdapter. I have an activity that hosts 3 fragments via a TabLayout. Here is the ViewPager Adapter:
`public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new LiveFragment();
case 1:
return new FollowingFragment();
case 2:
return new TrendingFragment();
default:
return new TrendingFragment();
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getString(R.string.trending_text);
case 1:
return getResources().getString(R.string.following_text);
case 2:
return getResources().getString(R.string.new_text);
default:
return getResources().getString(R.string.trending_text);
}
}
}`
I initially have data in Firebase, so when the fragment (LiveFragment) loads, the RecyclerView should first populate the data that is already in Firebase, and then any new data as well as it gets added. Right now, it is not populating any data neither initially nor when subsequent data is added to mPollsRef (the parameter in my FirebaseRecyclerAdapter). Here is the fragment that utilizes the RecyclerView:
`public class LiveFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private RecyclerView mRecyclerview;
private DatabaseReference mBaseRef;
private DatabaseReference mPollsRef;
private LinearLayoutManager mLayoutManager;
private FirebaseRecyclerAdapter<Poll, PollHolder> mFireAdapter;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public LiveFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment LiveFragment.
*/
// TODO: Rename and change types and number of parameters
public static LiveFragment newInstance(String param1, String param2) {
LiveFragment fragment = new LiveFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBaseRef = FirebaseDatabase.getInstance().getReference();
mPollsRef = mBaseRef.child("Polls");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment_new, container, false);
Log.v("TAG", "ON CREATE CALLED FROM NEW");
mPollsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.v("TAG", dataSnapshot.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mRecyclerview = (RecyclerView) v.findViewById(R.id.new_RecyclerView);
mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerview.setLayoutManager(mLayoutManager);
mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mBaseRef.child("Polls")) {
@Override
protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) {
viewHolder.mPollQuestion.setText(model.getQuestion());
Picasso.with(getActivity().getApplicationContext())
.load(model.getImage_URL())
.fit()
.into(viewHolder.mPollImage);
Log.v("TAG", model.getQuestion());
Log.v("TAG", model.getImage_URL());
}
};
mRecyclerview.setAdapter(mFireAdapter);
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
if (mFireAdapter != null) {
mFireAdapter.cleanup();
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public static class PollHolder extends RecyclerView.ViewHolder {
TextView mPollQuestion;
ImageView mPollImage;
public PollHolder(View itemView) {
super(itemView);
mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}`
What I have tried so far based on what I have read:
-
Does it make a difference whether I use FragmentPagerAdapter or FragmentStatePagerAdapter in the host activity?
-
I have changed the mRecyclerView to have a layout_width and layout_height to match parent, as I understand based on previous posts that it was a known bug.
-
When the fragment loads, I have data in Firebase already, so right off the bat the RecyclerView should populate the data that is initially stored in Firebase (before adding any new activity). Is it possible that the mPollsRef is being recognized as null?
-
I am seeing the error:
12-17 09:35:37.402 2919-2919/com.troychuinard.fanpolls E/RecyclerView: No adapter attached; skipping layout
-
I have removed the .hasFixedSize(true) from my RecyclerView, as I understand that is a known bug/issue with the FirebaseRecyclerAdapter
-
I have “TAGS” in my logging for populateViewHolder(), and it clearly isn’t being called.
-
It looks like my mRecyclerView is null? See image below. Did some debugging to verify.
I will edit as I continue to add thoughts/comments on what I have tried. Any help would be appreciated.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
Hey @troy21688, I sent you a PR on bitbucket. It took me a while to figure out, but it was a classic recyclerview mistake: make sure your
layout_height="match_parent"
! (or at least don’t usesetHasFixedSize(true)
with wrap content)@samtstern 😂 thanks!