Question

I have a ListView that I want to populate upon returning to an Activity. However upon returning I'd like to get updated values for my list. I have an Activity and a Fragment which belongs to that Activity (the list view is contained within the fragment).
Would I set a ListAdapter in the Activity Class or the Fragment Class? Also is one better practice than the other?

Thanks for any help!

Was it helpful?

Solution

You can technically do either, but since the fragment manages the listview, you should set it there. Doing it through activity forces the activity to find the fragment first. It's a layer of indirection you don't need.

It also makes your fragment more modular, since the two pieces of code travel together now (it handles it's own adapter), if you decide to place the fragment in new activity class.

OTHER TIPS

Best approach is to set adapter from fragment than in activity. You can create a custom fragment which extends ListFragment and setListAdapter(new ArrayAdapter) .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top