My custom adapter is extending ListAdapter for displaying a list of items in a RecyclerView. Everything works fine from MainActivity but when I try to display inside a Fragment, the RecyclerView is blank.
Here's the excerpt from MainActivity, which works fine for that Activity and displays the whole list of items in the RecyclerView.
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final GamebookCustomAdapter adapter = new GamebookCustomAdapter();
recyclerView.setAdapter(adapter);
gamebookViewModel =
ViewModelProviders.of(this).get(GamebookViewModel.class);
gamebookViewModel.getAllGamebooks().observe(this, new
Observer<List<Gamebook>>() {
@Override
public void onChanged(@Nullable List<Gamebook> gamebooks) {
adapter.submitList(gamebooks);
}
});
Is there something extra I need to provide in order for this code to work inside a Fragment?
Previously I'd been extending RecyclerView.Adapter in my custom adapter and declaring an ArrayList for the items in my list. This had worked fine with the Fragment. But when I extended ListAdapter and did away with the ArrayList, the RecyclerView shows up blank in the Fragment, though it still works fine in MainActivity. What am I missing?