Firebaseui-android: RecyclerView adapter does not work in fragment but works in activity

Created on 25 Apr 2017  ยท  11Comments  ยท  Source: firebase/FirebaseUI-Android

I've been using activity and firebase-ui in order to populate RecyclerView with data from Realtime Database. It works well in activity like this:

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_transactions);
        list = (RecyclerView) findViewById(R.id.list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);
        list.setAdapter(new TransactionsAdapter(
                DataManager.getTransactionsRef().orderByChild("date")
        ));
    }

but then I moved this code to fragment with the same layout:

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        list.setLayoutManager(layoutManager);
        list.setAdapter(new TransactionsAdapter(
                DataManager.getTransactionsRef().orderByChild("date")
        ));
    }

and it does not show any data. Currently I have both Activity and Fragment and it works in activity but it doesn't in Fragment.

Here is my dependencies:

    compile 'com.google.firebase:firebase-core:10.2.1'
    compile 'com.google.firebase:firebase-database:10.2.1'
    compile 'com.google.firebase:firebase-auth:10.2.1'
    compile 'com.firebaseui:firebase-ui-auth:1.2.0'
    compile 'com.firebaseui:firebase-ui-database:1.2.0'
    compile 'com.android.support:design:25.3.1'

Most helpful comment

@semenoh This question would be better for Stack Overflow, but I'll give you some clues here anyway.

Ok, so you know how in your AppCompatActivity you use setContentView(R.layout.something)? Well, in Fragments you have to deal with that stuff yourself. I would recommend Ctrl + Bing the super.onViewCreated(view, savedInstanceState); to see what it does. If you do that, you'll see that it returns null and does nothing:

@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return null;
}

TLDR: you have to manually inflate your layout like so:

View rootView = inflater.inflate(R.layout.something, container, false);

// ...
rootView.findViewById(R.id.recycler_view).setAdapter(...)

return rootView;

Hope this helps! ๐Ÿ˜„

All 11 comments

@semenoh This question would be better for Stack Overflow, but I'll give you some clues here anyway.

Ok, so you know how in your AppCompatActivity you use setContentView(R.layout.something)? Well, in Fragments you have to deal with that stuff yourself. I would recommend Ctrl + Bing the super.onViewCreated(view, savedInstanceState); to see what it does. If you do that, you'll see that it returns null and does nothing:

@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return null;
}

TLDR: you have to manually inflate your layout like so:

View rootView = inflater.inflate(R.layout.something, container, false);

// ...
rootView.findViewById(R.id.recycler_view).setAdapter(...)

return rootView;

Hope this helps! ๐Ÿ˜„

@SUPERCILEX thanks for the pointers!

@semenoh as mentioned, when the question is specific to your app (and not a bug in the library, which I am pretty sure this is not) it's better asked on StackOverflow.

Sorry I missed that part when creating an issue. Actually my code looks like this:

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_transactions, container, false);

        list = (RecyclerView) v.findViewById(R.id.list);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        list.setLayoutManager(layoutManager);
        list.setAdapter(new TransactionsAdapter(
                DataManager.getTransactionsRef().orderByChild("date")
        ));

    }

So actually I'm inflating view and issue is not related to my app but to the library.

@semenoh if you believe it's a problem with the library please supply a MCVE.

Also I think using onViewCreated is not the correct choice here. That method is called immediately after onCreateView and there's no guarantee that getActivity() will be non-null.

You want to use Fragment#onActivityCreated

@semenoh Yeah, like @samtstern said, putting stuff in onViewCreated a little weird. I would just put everything in onCreateView and use the getContext() method for your layout manager.

Here are just a few pointers that might help you figure it out:

  1. Debug your app and make sure everything you think is being called is actually being called
  2. How are you adding the Fragment? If you are doing it dynamically, make sure you have a container view you are putting it in.
  3. Make sure all of your layout widths/heights are good

Hope this helps a little! ๐Ÿ˜„

@SUPERCILEX Thanks a lot, it really helped me a lot! And really sorry for rising this issue. It was my mistake in layout. 0dp height in ConstraintLayout was a root of problem.

@semenoh Glad you figured it out!!! ๐Ÿ˜„ Yeah, I still make mistakes with 0dp children in ConstraintLayout (and I converted my codebase way back in alpha2!). Basically, your child has to be constrained on both sides (top/bottom or left/right) _and_ the parent's size has to be stable.

OMG you guys dint know how much i love you at this moment , the fuking 0dp.

่ˆ…ๆœไฝ 

I am using Relative Layout Still the same error is there anyone can help?

In my case, I am using LinearLayout as a container for the fragment to display RecyclerView. If I set the height of RecyclerView or LinearLayout to 100dp its displays data. But when it is match_parent or wrap_content then it is not displaying anything. Please help me to solve this.

Was this page helpful?
0 / 5 - 0 ratings