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.

RecyclerView adapter does not work in fragment but works in activity

See original GitHub issue

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'

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
folk113commented, Sep 13, 2018

舅服你

6reactions
SUPERCILEXcommented, Apr 25, 2017

@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! 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

RecyclerView does not show items in Fragment, but works in ...
I am not sure why the items are not showing up in the RecyclerView. Upon debugging, it seems that the Fragment_Upcoming_Activities class does ......
Read more >
Using the RecyclerView | CodePath Android Cliffnotes
Here we need to create the adapter which will actually populate the data into the RecyclerView. The adapter's role is to convert an...
Read more >
RecyclerView inside Fragment with Android Studio - Medium
This class will call the layout that consists of the RecyclerView and connect the adapter with the RecyclerView. onViewCreated() will set the ...
Read more >
Create dynamic lists with RecyclerView - Android Developers
When an item scrolls off the screen, RecyclerView doesn't destroy its view. Instead, RecyclerView reuses the view for new items that have scrolled...
Read more >
Recyclerview in Fragment Android Studio Tutorial - YouTube
Your browser can 't play this video. Learn more. Switch camera.
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