Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid PagerAdapter not calling instantiateItem
    primarykey
    data
    text
    <p>I'm trying to implement ViewPager with PagerAdapter in my app. I'm mainly working off <a href="http://blog.stylingandroid.com/archives/537" rel="noreferrer">this example</a>.</p> <p>My <code>PagerAdapter</code> is called DataPagerAdapter and uses the following constructor and <code>instantiateItem</code>, where I inflate a view from <code>data_list_view.xml</code>:</p> <pre><code>public class DataPagerAdapter extends PagerAdapter implements TitleProvider { private LayoutInflater mInflater; private static String[] titles = new String[] { "Page 1", "Page 2", "Page 3" }; private final Context context; public DataPagerAdapter( Context context ) { this.context = context; } @Override public String getTitle( int position ) { return titles[ position ]; } @Override public int getCount() { return titles.length; } @Override public Object instantiateItem( View pager, int position ) { ListView v = (ListView) mInflater.inflate(R.layout.data_list_view, null); ((ViewPager)pager).addView( v, 0 ); return v; } @Override public void destroyItem( View pager, int position, Object view ) { ((ViewPager)pager).removeView( (ListView)view ); } @Override public boolean isViewFromObject( View view, Object object ) { return view.equals( object ); } @Override public void finishUpdate( View view ) {} @Override public void restoreState( Parcelable p, ClassLoader c ) {} @Override public Parcelable saveState() { return null; } @Override public void startUpdate( View view ) {} } </code></pre> <p>And in my activity I get my adapter:</p> <pre><code>DataPagerAdapter adapter = new DataPagerAdapter( this ); </code></pre> <p>And then later I want to refer to my dataListView in code:</p> <pre><code>dataListView = (ListView) findViewById(R.id.listViewData); </code></pre> <p>Problem is, the <code>dataListView</code> is returned as null by the <code>findViewById</code> call since the <code>instantiateItem</code> is never processed. </p> <p>When is <code>instantiateItem</code> called and is this done automatically (as the example seems to suggest) or do I have to force it somehow? </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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