I am inflating Views (not fragments) in my viewpager implementation.
My main class extends Activity.
Page 1 of the viewpager inflates a view made up of textviews. Page 2 inflates an achartengine piechart. All works well.
Page 3 is supposed to inflate a listview with a custom adapter. However, as I’ve only extended Activity, setListAdapter isn’t available.
If I change the main class to extend ListActivity, then the entire activity fails on load because it can’t find the android.R.id.list view (because it hasn’t been inflated yet)
This is the instantiate code I am using
public class SummaryPagerAdapteri extends PagerAdapter{
@Override
public int getCount() {
return 5;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
View view = null;
switch (position) {
case 0:
//find and inflate the view
resId = R.layout.recordsummary_one;
view = inflater.inflate(resId, null);
//do stuff
break;
case 1:
//find and inflate the view
resId = R.layout.recordsummary_two;
view = inflater.inflate(resId, null);
Context ctx = view.getContext();
//do stuff
break;
case 2:
resId = R.layout.recordsummary_three;
view = inflater.inflate(resId, null);
Context tctx = view.getContext();
if(pdn != null){
adapter = new SummaryNotesAdapter(tctx,pdn);
setListAdapter(adapter);
}
break;
case 3:
resId = R.layout.recordsummary_four;
view = inflater.inflate(resId, null);
//ToDo
break;
case 4:
resId = R.layout.recordsummary_five;
view = inflater.inflate(resId, null);
//ToDo
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
Any ideas on how I can use setListAdapter(adapter) without extending ListView, or how I can extend ListView without the activity failing when it can’t find the uninflated view on first load?
Many thanks
Dave
Inflate a regular ListView for page 3 and call ListView.setAdapter() to set the adapter. In your code this will either be:
or