android 列表viewpager,Android ViewPager and ListViews

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(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{

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值