Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>ListView</code> on the second page is doubly populated precisely because of <a href="https://stackoverflow.com/a/9714866/438140">my answer</a> given in the other question.</p> <p>You are assuming <code>instantiateItem</code> will be called only for the currently active page, whereas it will actually be called to preload for offscreen pages too. The reference <code>v</code> to the view will be overwritten by the second call to <code>instantiateItem</code>.</p> <p>Have a look at this example project to see one way how to do it with fragments: <a href="https://github.com/antonyt/ListFragmentViewPagerExample/tree/master/src/com/example/listfragment" rel="nofollow noreferrer">https://github.com/antonyt/ListFragmentViewPagerExample/tree/master/src/com/example/listfragment</a></p> <p>Activity:</p> <pre><code>public class ListFragmentViewPagerActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new ExamplePagerAdapter(getSupportFragmentManager())); } public class ExamplePagerAdapter extends FragmentPagerAdapter { public ExamplePagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return 5; } @Override public Fragment getItem(int position) { Fragment fragment = new ExampleListFragment(); // set arguments here, if required Bundle args = new Bundle(); // args.put(...) fragment.setArguments(args); return fragment; } } } </code></pre> <p>Fragment:</p> <pre><code>public class ExampleListFragment extends ListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // normally you should inflate a view here and save references // using ListFragment default layout for this example return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onStart() { super.onStart(); new CreateArrayListTask().execute(); } private class CreateArrayListTask extends AsyncTask&lt;String, Void, ArrayList&lt;String&gt;&gt; { @Override protected ArrayList&lt;String&gt; doInBackground(String... params) { ArrayList&lt;String&gt; linkArray = new ArrayList&lt;String&gt;(); linkArray.add("Item 1"); linkArray.add("Item 2"); return linkArray; } protected void onPostExecute(ArrayList&lt;String&gt; linkArray) { ArrayAdapter&lt;String&gt; a = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, linkArray); setListAdapter(a); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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