question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

populateViewHolder() Not Being Called; RecyclerView Not Loading

See original GitHub issue

I 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:

  1. Does it make a difference whether I use FragmentPagerAdapter or FragmentStatePagerAdapter in the host activity?

  2. 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.

  3. 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?

  4. I am seeing the error: 12-17 09:35:37.402 2919-2919/com.troychuinard.fanpolls E/RecyclerView: No adapter attached; skipping layout

  5. I have removed the .hasFixedSize(true) from my RecyclerView, as I understand that is a known bug/issue with the FirebaseRecyclerAdapter

  6. I have “TAGS” in my logging for populateViewHolder(), and it clearly isn’t being called.

  7. It looks like my mRecyclerView is null? See image below. Did some debugging to verify.

image

I will edit as I continue to add thoughts/comments on what I have tried. Any help would be appreciated.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
SUPERCILEXcommented, Dec 17, 2016

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 use setHasFixedSize(true) with wrap content)

0reactions
SUPERCILEXcommented, Mar 1, 2018

@samtstern 😂 thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

firebase - populateViewHolder not executing with ...
My main problem is that the populateViewHolder method is never called, so I got nothing displaying in the RecyclerView . I followed the...
Read more >
Firebase: RecyclerAdapter - LearnHowToProgram.com
Inside of the populateViewHolder() method, we call the bindRestaurant() method on our viewHolder to set the appropriate text and image with the given...
Read more >
RecyclerView.Adapter - Android Developers
Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state.
Read more >
firebase recycler adapter tutorial || On Populate ... - YouTube
In this video learn to bind view using Oncreate viewholder and OnBindView holder method .In this video i am showing how to bind...
Read more >
Picasso does not load image in fragment with RecyclerView
I tried hard coded URL but it is still not working. ... category) { @Override protected void populateViewHolder(MenuViewHolder viewHolder, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found