Welcome to the Treehouse Community
Looking to learn something new?
Kenneth Gray1,593 Points
Specifying Contexts from Fragments
The solution for obtaining a context in this video (using getListView().getContext()) depends on the fact that that the Fragments we're using are ListFragments. In the case of constructing ArrayAdapters that doesn't matter because the ListFragment is the whole reason we're bothering with the ArrayAdapter. But for AlertDialogs it's different.
What do/can you use if you're trying to create an AlertDialog from a regular Fragment (or doing anything else that requires a Context)? It seems like the getActivity() method is the solution. Is there a difference between the Context from getListView() and the Context constituted by getActivity()? Or are they exactly the same? I need a little more context for these Contexts.
Ben JakubenTreehouse Teacher
You are correct!
getActivity() will always give you the Activity that the Fragment is running in. And that's the context we're after.
getListView().getContext() will give us the same thing in this example...it's just a longer way of doing it. Looking back, I should probably substitute those calls for the simpler
getActivity(). We actually make this change in the Implementing Designs for Android project when converting ListViews to GridViews.