RecyclerView adapter does not work in fragment but works in activity
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:1
- Comments:11 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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 usesetContentView(R.layout.something)
? Well, in Fragments you have to deal with that stuff yourself. I would recommendCtrl + B
ing thesuper.onViewCreated(view, savedInstanceState);
to see what it does. If you do that, you’ll see that it returns null and does nothing:TLDR: you have to manually inflate your layout like so:
Hope this helps! 😄