How to set adapter in Fragment
-
25-12-2020 - |
Question
I've got a Fragment
whose onCreateView()
callback returns a custom View
. The View contains a GridView that I'd like to set an adapter on. Before I switched to using Fragments, I would simply find the GridView
by Id and then set the adapter
. For reasons I don't currently understand, the GridView
is null when I try finding it by Id in my FragmentsActivity
.
So, I'd like to set the adapter in the fragment
itself. I'm assuming I need to do this in onActivityCreated
, but I don't know how I'll set the adapter
on the GridView
that's contained within the View being returned by onCreateView. In other words, how do I grab hold of that View that's returned by onCreateView
to set the Adapter
on it?
EDIT - I just realized, I could probably just find the GridView
in onActivityCreated
and set the adapter
there, no?
La solution
For reasons I don't currently understand, the GridView is null when I try finding it by Id in my FragmentsActivity.
You created the GridView
in the fragment in onCreateView()
. Just hold onto it in a data member of the fragment.
Autres conseils
I know this answer comes a little late (and also I am quite new to Android). But what about using getView()
inside of onActivityCreated
, instead of finding it by ID?