3

I have a ViewPager with 3 pages. In each page, I would like to insert a listview which is populated in seperate classes. Now, I found the simplest method is by inflate layouts which has a fragment in it. Lets say I have 3 pages. In page 1 I inflate a layout with fragment1, in page 2 I inflate a layout with fragment2, and in page 3 I inflate a layout with fragment3. How do I assign activities as the fragments contents? I have been searching through the internet for tutorials but none really fits my needs. Or are there any better way to achieve my goal?

Illustration: enter image description here

Activity 1 2 and 3 are all ListViews. The inflate custom layouts as their itemslayout.

borislemke
  • 8,446
  • 5
  • 41
  • 54

1 Answers1

4

How do I assign activities as the fragments contents?

You don't. Activities hold fragments, not the other way around.

Or are there any better way to achieve my goal?

Use a ListFragment for each page of your ViewPager. Beyond that, you have not explained what your goal is, and therefore it is difficult to assist you further.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • See update, each activity is a list with custom layout as list items. – borislemke Jan 28 '12 at 23:13
  • @borislemke: My answer remains unchanged. What you want is not supported. Take your business logic from Activity 1 and put it on Fragment 1. Then, take your business logic from Activity 2 and put it on Fragment 2. Then, take your business logic from Activity 3and put it on Fragment 3. Delete Activity 1, Activity 2, and Activity 3 from your project. – CommonsWare Jan 28 '12 at 23:16
  • @borislemke: If, for some reason, you need the logic of Activity 1-3 to be available as separate activities (e.g., for use on a phone instead of a tablet), then have small stub activities that simply hold Fragments 1, 2, and 3 respectively. This way, your activities hold fragments, but you do not try to have fragments hold activities. – CommonsWare Jan 28 '12 at 23:19
  • Okay, so, if I inflate the layouts with no fragments, and instead the listviews directly, how do I populate the listview like I did in seperate activities? Can I still inflate custom layout as their list-items layout? – borislemke Jan 28 '12 at 23:21
  • @borislemke: Quoting the `ListFragment` documentation: "ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)" – CommonsWare Jan 28 '12 at 23:28
  • Mind to take a look at my code? This is a listview that I populated and would like to add to page1. How do I code this like what you meant in your comments? http://dl.dropbox.com/u/33331786/PlankTrainer/EasyListActivity.txt – borislemke Jan 28 '12 at 23:28
  • Can I like just change from extends ListActivity to FragmentList ? – borislemke Jan 28 '12 at 23:29
  • @borislemke: You will need to change `ListActivity` to `ListFragment`. You will also need to follow the instructions from the documentation that I quoted above. – CommonsWare Jan 28 '12 at 23:33
  • Thanks! I finally understand ListFragment now! Thank you very much! – borislemke Jan 28 '12 at 23:37
  • please give me suggestion..if any one have idea about it http://stackoverflow.com/q/13640264/336990 – CoDe Dec 01 '12 at 06:48