0

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?

James Whelan
  • 91
  • 1
  • 7
  • What is the implementation of the ` adapter.submitList(gamebooks)` method? – jbarat Jun 15 '19 at 19:23
  • I'm not sure what you mean by what is the implementation of submitList(), but isn't it a method belonging to the ListAdapter class, which differentiates between lists in the event of a change in the list? It's called on the adapter when a change occurs in the list, e.g. when an item is deleted from the list or when an item *within* a list item is changed, e.g. when an image is swapped out for another image. – James Whelan Jun 15 '19 at 19:34
  • You have a custom adapter called `GamebookCustomAdapter` inside that you have to have a method called `submitList` and maybe the issue is there. – jbarat Jun 15 '19 at 19:37
  • Thanks for your suggestion, but submitList() belongs to the ListAdapter class and is called on the adapter whenever it's needed. Because I'm extending ListAdapter in my custom adapter, surely there's no need to have a method called submitList() in the custom adapter? Since it's working fine in MainActivity, I can only assume it's got something to do with the fact that I'm trying to do this in a Fragment. – James Whelan Jun 15 '19 at 19:41
  • Have you tired to call `notifyDataSetChanged()` inside `onChanged` callback ? , or maybe init you're adapter ` GamebookCustomAdapter adapter = new GamebookCustomAdapter();` then set your data-set ` adapter.submitList(gamebooks);` then set your adapter to the recycler ` recyclerView.setAdapter(adapter);` ( All that inside `onChanged`) – Ben Shmuel Jun 15 '19 at 19:58
  • Way to go Ben! Calling setAdapter() inside the onChanged() method did the trick. The RecyclerView is working perfectly now inside the Fragment. Thanks so much! Why do you think this needed to be done for the Fragment though and not for the Activity? – James Whelan Jun 15 '19 at 20:15

0 Answers0