Note that there are some explanatory texts on larger screens.

plurals
  1. POFragmentPagerAdapter with an ArrayListFragment
    text
    copied!<p>EDIT: I figured it out, thanks</p> <p>I have been trying forever now to implement separate ListViews in each of my ViewPager's pages. I understand <a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentListArray.html" rel="nofollow">this tutorial</a> pretty well, but it only creates one ListView that is used for each page. </p> <p>I would like to be able to have multiple ListViews in each page, from which I can control what happens onClick for each page's ListView. I have my app working with one ListView (like in the tutorial), but I have been struggling badly with multiple. </p> <p>My code is posted below and I have tried a ton of different things and I just need to move on in my development. Thanks for the help</p> <pre><code>private static class MyAdapter extends FragmentPagerAdapter implements TitleProvider { public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return ArrayListFragment.newInstance(position); } @Override public int getCount() { return Arrays.TITLES.length; } public String getTitle(int position) { return Arrays.TITLES[position]; } } public static class ArrayListFragment extends ListFragment { int mNum; /** * Create a new instance of CountingFragment, providing "num" as an * argument. */ static ArrayListFragment newInstance(int num) { ArrayListFragment f = new ArrayListFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } /** * When creating, retrieve this instance's number from its arguments. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.list_layout, container, false); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, Arrays.accTEAMS)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Log.i("FragmentList", "Item clicked: " + id); } } </code></pre> <p>This is one of my ListFragments:</p> <pre><code>public class ACCfragment extends ListFragment { View v; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.list_layout, null); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, Arrays.accTEAMS); setListAdapter(adapter); } @Override public void onListItemClick(ListView l, View v, int position, long id) { } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload