I'm relatively new to Android development and development overall. I'm having trouble grasping the syntax/process for ViewPager.
I have several ListViews that I want to implement into a ViewPager. I've got the compatibility pack loaded and everything. But I haven't seen any complete examples of how to do this.
I learn best by looking at examples. If someone can post an examples of any projects you've implemented this sort of thing in, please let me know.
The issue is that I get a Null Pointer Exception on this line when trying to launch my activity:
listView1.setAdapter(new ArrayAdapter
I suspect that I'm just doing this all wrong. If I don't use the ViewPager, I can get both lists to display their content. So I know the lists aren't null...
EDIT:
Thanks to VenomM for the answer below! Here's the code I ended up using, slightly modified from VenomM's examples.
ViewPagerAdapter:
public class ViewPagerAdapter extends PagerAdapter implements TitleProvider { private ListView listView1; private static String[] titles = new String[] { "Page 1", "Page 2", "Page 3", }; private final Context context; public ViewPagerAdapter( 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 collection, int position) { LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater(); listView1 = (ListView) layoutInflater.inflate(R.layout.listview1, null); String[] listData = null; MyArrayAdapter dataAdapter; if (position == 0) { listData = context.getResources().getStringArray(R.array.list1); dataAdapter = new MyArrayAdapter((Activity) context, R.layout.rowlayout, listData); } else if (position == 1) { listData = context.getResources().getStringArray(R.array.list2); dataAdapter = new MyArrayAdapter((Activity) context, R.layout.rowlayout, listData); } else { listData = context.getResources().getStringArray(R.array.list3); dataAdapter = new MyArrayAdapter((Activity) context, R.layout.rowlayout, listData); } listView1.setAdapter(dataAdapter); listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> adapter, View view, int position, long arg3) { Toast.makeText(context, adapter.getAdapter().getItem(position).toString(), Toast.LENGTH_LONG).show(); } }); ((ViewPager) collection).addView(listView1, 0); return listView1; } @Override public void destroyItem(View collection, int position, Object view) { System.out.println("on destroyItem()"); ((ViewPager) collection).removeView((ListView) view); } @Override public boolean isViewFromObject(View view, Object object) { System.out.println("on isViewFromObject()"); return view == ((ListView) 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 ) {} }
ArrayAdapter:
public class MyArrayAdapter extends ArrayAdapter{ private Activity context = null; private String[] names = null; private int rowLayoutId; public MyArrayAdapter(Activity context, int textViewResourceId, String[] names) { super(context, textViewResourceId, names); this.context = context; this.names = names; this.rowLayoutId =textViewResourceId; } // static to save the reference to the outer class and to avoid access to // any members of the containing class static class ViewHolder { protected ImageView imageView; protected TextView textView; } }