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<Object>(this, R.layout.rowlayout, list1));
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<String>{
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;
}
}
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.
Since ViewPager2 makes use of RecyclerView as adapter to show items we can customize it's performance and design in many ways. We can make use of DiffUtils, layout direction such as right-to left, both vertical and horizontal direction to show items, PageTransformations etc.
I still can't understand why you change your Arrayadapter with object argument, if you want it to hold string items. Try changing
new ArrayAdapter<Object>(this, R.layout.rowlayout, list1)
to
new ArrayAdapter<String>(this, R.layout.rowlayout, list1)
I used a custom ArrayAdapter, everything worked fine for me. Please let me know if you succeed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With